对象在移动设备中不随鼠标移动而正确移动

Object is not moving proper with mouse movement in mobile devices

我正在尝试使用对象上的 pressmove 事件用鼠标移动对象。在 PC 和平板电脑上运行良好。但是,在移动设备中,它不能很好地工作,因为它们具有不同的宽度和高度。

您可以查看以下内容URL:- http://quirktools.com/screenfly/#u=http://saurabhysecit.byethost15.com/scratchGame_Canvas.html&w=320&h=568&a=37&s=1

直接URL是-http://saurabhysecit.byethost15.com/scratchGame_Canvas.html

它是从 Animate CC 生成的。 这些是下面的代码。

JS 代码 -

(function (lib, img, cjs, ss, an) {

var p; // shortcut to reference prototypes
lib.ssMetadata = [];

function mc_symbol_clone() {
    var clone = this._cloneProps(new this.constructor(this.mode, this.startPosition, this.loop));
    clone.gotoAndStop(this.currentFrame);
    clone.paused = this.paused;
    clone.framerate = this.framerate;
    return clone;
}

function getMCSymbolPrototype(symbol, nominalBounds, frameBounds) {
    var prototype = cjs.extend(symbol, cjs.MovieClip);
    prototype.clone = mc_symbol_clone;
    prototype.nominalBounds = nominalBounds;
    prototype.frameBounds = frameBounds;
    return prototype;
    }


(lib.coin = function(mode,startPosition,loop) {
    this.initialize(mode,startPosition,loop,{});

    // Layer 1
    this.shape = new cjs.Shape();
    this.shape.graphics.f().s("#AA8801").ss(1,2,1).p("AH5AAQAACrhhCBQgIAKgIAKQgQAUgTASQiKCLjAAKQgNAAgOAAQi5AAiKh2QgRgOgQgRQiUiTAAjTQAAjDCBiOIAAgBQAJgKAKgKQAUgUAVgSQB9hlCigIQAOgBAOAAQAaAAAZACQAHAAAHABQCoAUB8B9QATASAQAUQBtCEAECvQAAADAAACQAAAFAAADg");
    this.shape.setTransform(-4.8,0.1);

    this.shape_1 = new cjs.Shape();
    this.shape_1.graphics.f("#D9AE01").s().p("AlbGiQgTgQgSgSQigieAAjiQAAjhCgifQAWgWAXgTQCRh2DCgBQAdABAbACQC+ARCLCMQAUATASAWQB6CTAADEQAADGh6CSQgSAVgUATQiUCWjQAKIgdAAQjHAAiUh+gAgqnnQijAIh8BmQgVARgUAUIgUAUIAAABQiBCOAADEQAADSCVCTQAQARARAOQCJB2C6AAIAbAAQDAgJCJiMQATgSAQgUIAQgUQBhiBAAiqIAAgJIAAgGQgEithtiFQgQgTgTgTQh7h9ipgTIAAgDQgYgCgaAAQgVAAgVACg");
    this.shape_1.setTransform(-3.3,-1.7);

    this.shape_2 = new cjs.Shape();
    this.shape_2.graphics.f("#FFCC00").s().p("AlDGFQgQgOgQgQQiViUAAjSQAAjDCBiPIAAAAIAUgVQAUgUAUgRQB9hmCigIQAWgCAVAAQAZAAAZACIAAADIgNgCQgZgCgbAAIgcABIAcgBQAbAAAZACIANACQCoATB8B9QATATAQAUQBsCEAFCvIAAAFIAAAIQAACqhhCCIgQATQgQAUgTATQiKCLjAAJIgbABQi5AAiKh3g");
    this.shape_2.setTransform(-4.8,0.1);

    this.shape_3 = new cjs.Shape();
    this.shape_3.graphics.f("#695400").s().p("Ah6I1IgBAAQjHgKiUiJQCUB+DIAAIAeAAQDOgKCUiWQAUgTASgWQB6iSAAjFQAAjEh6iTQgSgWgUgUQiKiLi+gRQgbgDgdAAQjCAAiSB3QCSiBDBgKIABAAIAfAAIAbAAIAAAAQDXAJCbCcIAaAbIACAEQCJCcAADUIAAACQgBDViICaIgCAEQgMANgOANQikCmjpAAIgfAAg");
    this.shape_3.setTransform(9,-1.7);

    this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_3},{t:this.shape_2},{t:this.shape_1},{t:this.shape}]}).wait(1));

}).prototype = getMCSymbolPrototype(lib.coin, new cjs.Rectangle(-57.8,-58.2,113.9,113.1), null);


// stage content:
(lib.scratchGame_Canvas = function(mode,startPosition,loop) {
if (loop == null) { loop = false; } this.initialize(mode,startPosition,loop,{});

    // timeline functions:
    this.frame_0 = function() {
        var pressBool=false;
        var mask_mc = new createjs.Shape();
        var bg_mc = new createjs.Bitmap("images/YLogo1.jpg");
        //var coin_mc = new lib.coin();
        var coin_mc = new createjs.Shape(new createjs.Graphics().beginFill("#FFFFFF").drawCircle(0, 0, 50));

        coin_mc.x = stage.canvas.width/2;
        coin_mc.y = stage.canvas.width/2;

        stage.addChild(bg_mc, coin_mc);
        createjs.Touch.enable(stage, false, true);

        if(sRatio<1){
            coin_mc.scaleX = coin_mc.scaleY = sRatio;
        }
        coin_mc.addEventListener('pressmove', moveCoin.bind(this));
        coin_mc.addEventListener('mouseup', releaseCoin.bind(this));
        updateCacheImage(false);

        function moveCoin(e){       
            e.currentTarget.x = e.stageX;
            e.currentTarget.y = e.stageY;
            stage.update();
            createMask(e);
        }

        function createMask(e) {
            if(!pressBool)return;
            var xLoc = stage.mouseX-20;
            var yLoc = stage.mouseY-30;
            mask_mc.graphics.beginFill("FFFFFF").drawEllipse(xLoc, yLoc, 40, 60);
            updateCacheImage(true);
        }
        function updateCacheImage(update){
            var w = 361;
            if (update) {
                mask_mc.updateCache();
            } else {
                mask_mc.cache(0, 0, w, w);
            }

            maskFilter = new createjs.AlphaMaskFilter(mask_mc.cacheCanvas);

            bg_mc.filters = [maskFilter];
            if (update) {
                bg_mc.updateCache(0, 0, w, w);
            } else {
                bg_mc.cache(0, 0, w, w);
            }
        }
        function releaseCoin(e) {
            //stage.canvas.style.cursor = "default";
            pressBool = false;
            updateCacheImage(true);
        }
    }

    // actions tween:
    this.timeline.addTween(cjs.Tween.get(this).call(this.frame_0).wait(1));

}).prototype = p = new cjs.MovieClip();
p.nominalBounds = new cjs.Rectangle(385.9,359.4,113.9,113);
// library properties:
lib.properties = {
    width: 550,
    height: 400,
    fps: 24,
    color: "#999999",
    opacity: 1.00,
    manifest: [],
    preloads: []
};
})(lib = lib||{}, images = images||{}, createjs = createjs||{}, ss = ss||{}, AdobeAn = AdobeAn||{});
var lib, images, createjs, ss, AdobeAn;

HTML Code:- 
<!DOCTYPE html>
<!--
    NOTES:
    1. All tokens are represented by '$' sign in the template.
    2. You can write your code only wherever mentioned.
    3. All occurrences of existing tokens will be replaced by their appropriate values.
    4. Blank lines will be removed automatically.
    5. Remove unnecessary comments before creating your template.
-->
<html>
<head>
<meta charset="UTF-8">
<meta name="authoring-tool" content="Adobe_Animate_CC">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui" />
    <meta name="msapplication-tap-highlight" content="no"/>
<title>scratchGame_Canvas</title>
<!-- write your code here -->
<script src="jquery-3.2.1.slim.min.js"></script>
<script src="createjs-2015.11.26.min.js"></script>
<script src="scratchGame_Canvas.js?1497868121984"></script>
<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, fnStartAnimation;
var pRatio = window.devicePixelRatio || 1, xRatio, yRatio, sRatio=1;
function init() {
    canvas = document.getElementById("canvas");
    anim_container = document.getElementById("animation_container");
    dom_overlay_container = document.getElementById("dom_overlay_container");
    handleComplete();
}
function handleComplete() {
    //This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
    exportRoot = new lib.scratchGame_Canvas();
    stage = new createjs.Stage(canvas);
    stage.addChild(exportRoot); 
    //Registers the "tick" event listener.
    fnStartAnimation = function() {
        createjs.Ticker.setFPS(lib.properties.fps);
        createjs.Ticker.addEventListener("tick", stage);
    }       
    //Code to support hidpi screens and responsive scaling.
    function makeResponsive(isResp, respDim, isScale, scaleType) {      
        var lastW, lastH, lastS=1;      
        window.addEventListener('resize', resizeCanvas);
        resizeCanvas();     
        function resizeCanvas() {
            var w = lib.properties.width, h = lib.properties.height;
            var iw = window.innerWidth, ih=window.innerHeight;
            pRatio = window.devicePixelRatio || 1, xRatio=iw/w, yRatio=ih/h, sRatio=1;
            if(isResp) {                
                if((respDim=='width'&&lastW==iw) || (respDim=='height'&&lastH==ih)) {                    
                    sRatio = lastS;                
                }               
                else if(!isScale) {                 
                    if(iw<w || ih<h)                        
                        sRatio = Math.min(xRatio, yRatio);              
                }
                else if(scaleType==1) {
                    sRatio = Math.min(xRatio, yRatio);
                }
                else if(scaleType==2) {
                    sRatio = Math.max(xRatio, yRatio);              
                }
            }
            canvas.width = w*pRatio*sRatio;         
            canvas.height = h*pRatio*sRatio;
            canvas.style.width = dom_overlay_container.style.width = anim_container.style.width =  w*sRatio+'px';               
            canvas.style.height = anim_container.style.height = dom_overlay_container.style.height = h*sRatio+'px';
            stage.scaleX = pRatio*sRatio;
            stage.scaleY = pRatio*sRatio;
            lastW = iw; lastH = ih; lastS = sRatio;     
        }
    }
    makeResponsive(true,'both',false,1);
    fnStartAnimation();
}
</script>
<!-- write your code here -->
</head>
<body onload="init();" style="margin:0px;">
    <div id="animation_container" style="background-color:rgba(153, 153, 153, 1.00); width:550px; height:400px">
        <canvas id="canvas" width="550" height="400" style="position: absolute; display: block; background-color:rgba(153, 153, 153, 1.00);"></canvas>
        <div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:550px; height:400px; position: absolute; left: 0px; top: 0px; display: block;">
        </div>
    </div>
</body>
</html>

Hope to get response soon.

Thanks.

正如我在评论中提到的,您可以转换坐标,因此您的 x/y 鼠标位置不受 canvas 响应缩放的影响,一种替代方法是使用,在绑定函数与 pressmove 事件(在你的例子中,函数 moveCoin()),globalToLocal() 方法。

scratchGame_Canvas.js

函数 moveCoin()

function moveCoin(e){   
    var point;

    point = stage.globalToLocal(e.stageX, e.stageY); 
    e.currentTarget.x = point.x;
    e.currentTarget.y = point.y;
    stage.update();
    createMask(e);
}

另请参阅在 Animate CC 2017 中使用 easeljs 进行拖放 & MovieClip class (EaselJS API)