Skip to main content
alexanderp79115696
Participant
February 4, 2017
Answered

How to convert this code to HTML 5 Canvas

  • February 4, 2017
  • 7 replies
  • 1574 views

drag_mc.buttonMode = true;

drag_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);

drag_mc.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

function startDragging(evt:MouseEvent):void {

    evt.target.startDrag(true);

}

function stopDragging(evt:MouseEvent):void {

    evt.target.stopDrag();

}

//test to see if objects intersect

stage.addEventListener(Event.ENTER_FRAME, checkHitArea);

function checkHitArea(evt:Event){

    if(this.hotspot_mc.hitTestObject(drag_mc)){

        trace("Hitting");

    }else{

        trace("Not hitting");

    }

}

    This topic has been closed for replies.
    Correct answer alexanderp79115696

    I can give you much better links with code , but it don't work in Adobe Animae CC.

    Here is what i need:

    var stage, containerDrag, dragOffset;

       

        function init(){

            //setup stage

            stage = new createjs.Stage('canvas');

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

            createjs.Ticker.setFPS(60);

           

            //drag container

            containerDrag = new createjs.Container();

            stage.addChild(containerDrag);

           

            //stage listeners

            stage.addEventListener("stagemousedown", startDrag);  

            stage.addEventListener("stagemouseup", stopDrag);

            dragOffset = new createjs.Point();

           

            //add random shape to container

            for(var i = 0; i < 100; i++){

                var rect = new createjs.Shape();

                rect.graphics.beginFill('#ffffff').drawRect(0,0,getRandom(1, 3), getRandom(1, 3));

                rect.x = getRandom(0, 1000);

                rect.y = getRandom(0, 1000);

                containerDrag.addChild(rect);

            }

         

        }   

       

        //draggable listeners

        function startDrag(){

            dragOffset.x = stage.mouseX - containerDrag.x;

            dragOffset.y = stage.mouseY - containerDrag.y;

            stage.addEventListener("stagemousemove", moveDrag);  

        }  

           

        function stopDrag(){

             stage.removeEventListener("stagemousemove", moveDrag);  

        }

           

        function moveDrag(e){

          containerDrag.x = e.stageX - dragOffset.x;

          containerDrag.y = e.stageY - dragOffset.y;

        }   

       

        //Random utility function   

        function getRandom(min, max){

            return min + (Math.random() * (max-min));

        } 

           

        function tick(e){

            stage.update();

        }

    in js file - it work perfect , but in Adobe Animate CC i see only white screen.

    And i need to replace draggable container to any symbol on my stage.

    7 replies

    Legend
    February 4, 2017
    alexanderp79115696
    alexanderp79115696AuthorCorrect answer
    Participant
    February 4, 2017

    I can give you much better links with code , but it don't work in Adobe Animae CC.

    Here is what i need:

    var stage, containerDrag, dragOffset;

       

        function init(){

            //setup stage

            stage = new createjs.Stage('canvas');

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

            createjs.Ticker.setFPS(60);

           

            //drag container

            containerDrag = new createjs.Container();

            stage.addChild(containerDrag);

           

            //stage listeners

            stage.addEventListener("stagemousedown", startDrag);  

            stage.addEventListener("stagemouseup", stopDrag);

            dragOffset = new createjs.Point();

           

            //add random shape to container

            for(var i = 0; i < 100; i++){

                var rect = new createjs.Shape();

                rect.graphics.beginFill('#ffffff').drawRect(0,0,getRandom(1, 3), getRandom(1, 3));

                rect.x = getRandom(0, 1000);

                rect.y = getRandom(0, 1000);

                containerDrag.addChild(rect);

            }

         

        }   

       

        //draggable listeners

        function startDrag(){

            dragOffset.x = stage.mouseX - containerDrag.x;

            dragOffset.y = stage.mouseY - containerDrag.y;

            stage.addEventListener("stagemousemove", moveDrag);  

        }  

           

        function stopDrag(){

             stage.removeEventListener("stagemousemove", moveDrag);  

        }

           

        function moveDrag(e){

          containerDrag.x = e.stageX - dragOffset.x;

          containerDrag.y = e.stageY - dragOffset.y;

        }   

       

        //Random utility function   

        function getRandom(min, max){

            return min + (Math.random() * (max-min));

        } 

           

        function tick(e){

            stage.update();

        }

    in js file - it work perfect , but in Adobe Animate CC i see only white screen.

    And i need to replace draggable container to any symbol on my stage.

    Legend
    February 4, 2017

    Rip out the parts of that code that you don't need in Animate. You don't need to create a stage. You don't need to create a ticker. You don't need to set the frame rate.