2018-08-09

var MvCvsSprite = function(ui, asset, sourceX, sourceY, sourceWidth, sourceHeight, x, y, width, height) {

    DisplayObject.bind(this)(ui, x, y, width, height);

    this.asset = asset;

    this.sourceX = sourceX

    this.sourceY = sourceY

    this.sourceWidth = sourceWidth;

    this.sourceHeight = sourceHeight;

    this.startY = undefined;

    this.moveY = 0;

    this.onMoveThis = this.onMove.bind(this);

    this.onUpThis = this.onUp.bind(this);   

    this.onClick(() => {

        console.info('You got me!');

        window.addEventListener('mousemove', this.onMoveThis);

        window.addEventListener('mouseup',this.onUpThis);

    });

};

MvCvsSprite.prototype = Object.create(DisplayObject.prototype);

MvCvsSprite.prototype.draw = function(context, x, y, width, height) {

    context.drawImage(this.asset, this.sourceX, this.sourceY, this.sourceWidth, this.sourceHeight, x, y, width, height);

};

// wx.onTouchMove(

MvCvsSprite.prototype.onMove = function (e) {

    var canvasPos = {x:0, y:0};

    canvasPos = ui.windowToUISpace(e.clientX, e.clientY)

    if (this.startY === undefined) {

        this.startY = canvasPos.y + this.moveY;

    }

    this.moveY = this.startY - canvasPos.y;

    console.log(this.moveY);

    this.sourceY = this.moveY;

    ui.shouldReDraw = true;

}

// wx.onTouchEnd(

MvCvsSprite.prototype.onUp = function (e) {

    this.startY = undefined;

    if (this.moveY < 0) { // 到顶

        this.moveY = 0;

    } else if (this.moveY > this.asset.height - this.height) { // 到底

        this.moveY = this.asset.height - this.height;

    }

    console.log(this.moveY);

    this.sourceY = this.moveY;

    ui.shouldReDraw = true;

    window.removeEventListener('mousemove', this.onMoveThis);

    window.removeEventListener('mouseup', this.onUpThis);

};

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,068评论 0 2
  • 找到fullcalendar.js, 找到代码为 isRTL:false,这句话 输入以下几句 monthName...
    迷你小小白阅读 1,740评论 0 1
  • 初等数学2300年之重大错误:将无穷多各异点集误为同一集 ——让中学生也能一下子认识3000年都无人能识的直线段 ...
    hxl268阅读 554评论 0 0
  • 1,换行 2,html图像通过标签 来定义 3,class为html 元素定义一个或者多个类名 (classNam...
    滔滔逐浪阅读 164评论 0 0
  • 晓晓是我的初恋女友,也是我的前任女友,更是我曾经认定会携手一生的女人。如今,她结婚了,老公不是我。婚后的她时常打来...
    三湘涟漪阅读 29,685评论 312 1,022