Skip to main content
March 3, 2019
해결됨

CreateJS : how to limit the movement of my clip to the size of my scene?

  • March 3, 2019
  • 1 답변
  • 636 조회

Hello!

I have a zoom system that works by scrolling the mouse. I would like however, that when I move in the zoom, the area is limited to the size of my scene.

Currently, I can bring out my image of the canas ...

I drew a rectangle of 1920/1080. So I want the image doesn't exceed this area.

The code and the demo online

//Référence du scénario

_this = this;

// Add a background

var bg = new createjs.Shape();

bg.graphics.beginStroke("#000");

bg.graphics.beginFill("#222");

bg.graphics.setStrokeStyle(1);

bg.graphics.drawRect(0, 0, 1920, 1080);

bg.alpha=0.5;

bg.setBounds(0, 0, 1920, 1080);

var bounds = bg.getBounds();

stage.addChild(bg);

//Mouse cursor

stage.enableMouseOver();

var dragStart = null;

//Gestion du touché sur tablette/mobile

createjs.Touch.enable(stage);

createjs.Ticker.addEventListener("tick", onTick);

stage.addEventListener('mousedown', onMouseDown);

stage.addEventListener('stagemousemove', onMouseMove);

stage.addEventListener('stagemouseup', onMouseUp);

stage.addEventListener('mouseleave', onMouseUp);

canvas.addEventListener('DOMMouseScroll', onMouseWheel, false);

canvas.addEventListener('mousewheel', onMouseWheel, false);

function zoom(delta){

        var p = _this.test.globalToLocal(stage.mouseX, stage.mouseY);

        var scale = Math.max(1, Math.min(_this.test.scaleX + delta, 2));

       _this.test.scaleX = _this.test.scaleY = scale;

        var p2 = _this.test.globalToLocal(stage.mouseX, stage.mouseY);

        if(_this.test.rotation == 0){

            _this.test.x += (p2.x - p.x) * scale;

            _this.test.y += (p2.y - p.y) * scale;

        }else if (_this.test.rotation == 90){

            _this.test.x -= (p2.y - p.y) * (scale);

            _this.test.y += (p2.x - p.x) * (scale);

        }else if(test.rotation == 180){

            _this.test.x -= (p2.x - p.x) * scale;

            _this.test.y -= (p2.y - p.y) * scale;

        }else{

            _this.test.x += (p2.y - p.y) * (scale);

            _this.test.y -= (p2.x - p.x) * (scale);

        }

    }

    function rotate(r){

        if((_this.test.rotation+r)  > 270){

            _this.test.rotation = 0;

        }else{

            _this.test.rotation += r;

        }

    }

function onMouseDown(){

  dragStart = new createjs.Point(stage.mouseX, stage.mouseY);

}

function onMouseMove(evt){

  if (dragStart){

    var mouse = new createjs.Point(stage.mouseX, stage.mouseY);

    var delta = new createjs.Point(mouse.x - dragStart.x, mouse.y - dragStart.y);

    dragStart = mouse;

    _this.test.x += delta.x;

    _this.test.y += delta.y;

  }

}

function onMouseUp(){

  dragStart = null;

}

function onMouseWheel(e){

  var delta = e.wheelDelta ? e.wheelDelta/100 : e.detail ? -e.detail : 0;

  if (delta) zoom(delta);

  return e.preventDefault() && false;

}

function onTick(){

  stage.update();

}

Thanks!

Matt

    이 주제는 답변이 닫혔습니다.
    최고의 답변: JoãoCésar17023019

    Hi.

    It seems your code is a CreateJS only solution.

    Anyway, the main problem is here:

    var scale = Math.max(1, Math.min(_this.test.scaleX + delta, 2));

    It should be:

    var scale = Math.min(1, Math.min(_this.test.scaleX + delta, 2));

    Because in the in the end you need your maximum scale to be 1.

    Another issue is that your delta value is coming either as 3 or -3 so the user doesn't really get to see a graduation when using the mouse wheel. You should use just a fraction of the delta value.

    And if you're developing in Animate CC, there's no need to call stage.update() in the tick handler function because ANCC will to this automatically for you.

    I hope this helps.

    Regards,

    JC

    1 답변

    March 3, 2019

    //Référence du scénario

    _this = this;

    // Add a background

    var bg = new createjs.Shape();

    bg.graphics.beginStroke("#000");

    bg.graphics.beginFill("#222");

    bg.graphics.setStrokeStyle(1);

    bg.graphics.drawRect(0, 0, 1920, 1080);

    bg.alpha=0.5;

    bg.setBounds(0, 0, 1920, 1080);

    var bounds = bg.getBounds();

    stage.addChild(bg);

    //Curseur souris

    stage.enableMouseOver();

    var dragStart = null;

    //Gestion du touché sur tablette/mobile

    createjs.Touch.enable(stage);

    createjs.Ticker.addEventListener("tick", onTick);

    stage.addEventListener('mousedown', onMouseDown);

    stage.addEventListener('stagemousemove', onMouseMove);

    stage.addEventListener('stagemouseup', onMouseUp);

    stage.addEventListener('mouseleave', onMouseUp);

    canvas.addEventListener('DOMMouseScroll', onMouseWheel, false);

    canvas.addEventListener('mousewheel', onMouseWheel, false);

    function zoom(delta){

            var p = _this.test.globalToLocal(stage.mouseX, stage.mouseY);

            var scale = Math.max(1, Math.min(_this.test.scaleX + delta, 2));

          _this.test.scaleX = _this.test.scaleY = scale;

            var p2 = _this.test.globalToLocal(stage.mouseX, stage.mouseY);

            if(_this.test.rotation == 0){

                _this.test.x += (p2.x - p.x) * scale;

                _this.test.y += (p2.y - p.y) * scale;

            }else if (_this.test.rotation == 90){

                _this.test.x -= (p2.y - p.y) * (scale);

                _this.test.y += (p2.x - p.x) * (scale);

            }else if(test.rotation == 180){

                _this.test.x -= (p2.x - p.x) * scale;

                _this.test.y -= (p2.y - p.y) * scale;

            }else{

                _this.test.x += (p2.y - p.y) * (scale);

                _this.test.y -= (p2.x - p.x) * (scale);

            }

        }

        function rotate(r){

            if((_this.test.rotation+r)  > 270){

                _this.test.rotation = 0;

            }else{

                _this.test.rotation += r;

            }

        }

    function onMouseDown(){

      dragStart = new createjs.Point(stage.mouseX, stage.mouseY);

    }

    function onMouseMove(evt){

      if (dragStart){

        var mouse = new createjs.Point(stage.mouseX, stage.mouseY);

        var delta = new createjs.Point(mouse.x - dragStart.x, mouse.y - dragStart.y);

        dragStart = mouse;

        _this.test.x += delta.x;

        _this.test.y += delta.y;

      }

    }

    function onMouseUp(){

      dragStart = null;

    }

    function onMouseWheel(e){

      var delta = e.wheelDelta ? e.wheelDelta/100 : e.detail ? -e.detail : 0;

      if (delta) zoom(delta);

      return e.preventDefault() && false;

    }

    function onTick(){

      stage.update();

    }

    JoãoCésar17023019
    Community Expert
    Community Expert
    March 4, 2019

    Hi.

    It seems your code is a CreateJS only solution.

    Anyway, the main problem is here:

    var scale = Math.max(1, Math.min(_this.test.scaleX + delta, 2));

    It should be:

    var scale = Math.min(1, Math.min(_this.test.scaleX + delta, 2));

    Because in the in the end you need your maximum scale to be 1.

    Another issue is that your delta value is coming either as 3 or -3 so the user doesn't really get to see a graduation when using the mouse wheel. You should use just a fraction of the delta value.

    And if you're developing in Animate CC, there's no need to call stage.update() in the tick handler function because ANCC will to this automatically for you.

    I hope this helps.

    Regards,

    JC

    March 5, 2019

    Thanks