EaselJS を使用して塗りつぶし (四角形) を作成し、その上にテキストをコンテナー内に作成するプロジェクトがあります。私の目的は、この長方形とテキストをドラッグしてキャンバス上を移動できるようにすることです。これはすでに行われており、うまく機能しています。
私の問題は、onMouseOver ハンドラーを使用しscaleX
て長方形のサイズを変更しようとするときです。scaleY
これは実際に行われますが、長方形は最初の点から別の場所に移動するだけです。
これをオーバーライドするには プロパティregX
とプロパティを使用する必要があると読みましたが、コードのコンテキストではできません。regY
私は何を間違っていますか?
これが私のJavascriptコードです:
(function(){
var stage, myCanvas;
var update = true;
this.init = function() {
myCanvas = document.getElementById("stageCanvas");
stage = new createjs.Stage(myCanvas);
stage.enableMouseOver();
stage.mouseEnabled = true;
stage.mouseMoveOutside = true;
// Reference Shape
var rectFijo0 = new createjs.Shape();
rectFijo0.graphics.beginFill("#DCD8D4").rect(140,160,78,35);
stage.addChild(rectFijo0);
// Shape
var rectOpt0 = new createjs.Shape();
rectOpt0.graphics.beginFill("#C51A76").rect(140,160,78,35);
txtOpt0 = new createjs.Text("TEST","bold 20px","#FFF");
txtOpt0.textAlign ="center";
txtOpt0.x = 50;
txtOpt0.y = 50;
// Container
var containerOpt0= new createjs.Container();
containerOpt0.mouseEnabled = true;
//#######
// Probably here is my problem. I don't understand why if I use regX and regY the rectangle moves the lower right corner to the center, instead of just using this point as a registration point. Why? What I am not understanding?
//containerOpt0.regX = 78/2;
//containerOpt0.regY = 35/2;
//#######
containerOpt0.onPress = pressHandler;
containerOpt0.onMouseOver = mouseOverHandler;
containerOpt0.addChild(rectOpt0, txtOpt0);
stage.addChild(containerOpt0);
stage.update();
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", tick);
}
function pressHandler(e){
// onPress Handler to drag
var offset = {x:e.target.x-e.stageX, y:e.target.y-e.stageY};
e.onMouseMove = function(ev) {
e.target.x = ev.stageX+offset.x;
e.target.y = ev.stageY+offset.y;
update = true;
}
}
function mouseOverHandler(e){
e.target.scaleX = .5;
e.target.scaleY = .5;
update = true;
}
function tick() {
if (update) {
update = false;
stage.update();
}
}
window.onload = init();
}());
これが私のJS Fiddleの例です。何が起こっているのかを正確に見ることができます。マウスを四角形の上にドラッグするだけで、私の言いたいことがわかります。簡単に達成できるはずですが、どうすればよいかわかりません。