pixi.js 实现Sprite跟随鼠标移动

pixi.js 实现Sprite跟随鼠标移动移动功能在官方实例中已有 不过在开发过程中因为与需求有个冲突 bug 所以又重新写了一个 虽然并没有直接解决不过也记录下吧 官方实例代码 obj on mousedown onDragStart on touchstart onDragStart on mouseup onDragEnd on mouseupoutsi onDragE

移动功能在官方实例中已有利用dragPoint实现,不过在开发过程中因为与需求有个冲突bug,所以又重新写了一个利用position.x\y实现的,虽然并没有直接解决不过也记录下吧。哎哎哎哎

官方实例代码:链接

obj.on('mousedown', onDragStart) .on('touchstart', onDragStart) .on('mouseup', onDragEnd) .on('mouseupoutside', onDragEnd) .on('touchend', onDragEnd) .on('touchendoutside', onDragEnd) .on('mousemove', onDragMove) .on('touchmove', onDragMove);
function onDragStart(event) { 
    if (!this.dragging) { this.data = event.data; this.dragging = true; this.scale.x *= 1.1; this.scale.y *= 1.1; this.dragPoint = event.data.getLocalPosition(this.parent); this.dragPoint.x -= this.x; this.dragPoint.y -= this.y; } } function onDragEnd() { 
    if (this.dragging) { this.dragging = false; this.scale.x /= 1.1; this.scale.y /= 1.1; this.data = null; } } function onDragMove() { 
    if (this.dragging) { var newPosition = this.data.getLocalPosition(this.parent); this.x = newPosition.x - this.dragPoint.x; this.y = newPosition.y - this.dragPoint.y; } }
var texture = PIXI.Texture.fromFrame("iamges/body.png"); var body = new Sprite(texture); var rotate = new Sprite( resources["./images/rotate.png"].texture ); var close = new Sprite( resources["./images/close.png"].texture ); var scale = new Sprite( resources["./images/zoom.png"].texture ); var border = new PIXI.Graphics(); // 绘制虚线轮廓 border.lineStyle(0.5, 0xFFFFFF, 0.5); border.drawRect(0, 0, body.width, body.height); border.endFill(); border.x = body.x - 10; border.y = body.y - 10; border.width = body.width + 20; border.height = body.height + 20; rotate.width = 32; rotate.height = 24; rotate.x = border.x + border.width; rotate.y = border.y; rotate.defaultstate = true; // 默认显示旋转按钮 close.width = 32; close.height = 24; close.x = border.x - close.width; close.y = border.y + border.height - close.height; scale.width = 32; scale.height = 24; scale.x = border.x + border.width; scale.y = border.y + border.height - scale.height; scale.defaultstate = true; // 默认显示缩放按钮 border.interactive = scale.interactive = close.interactive = rotate.interactive = body.interactive = true; var materialContainer = new PIXI.Container(); // 精灵分组 materialContainer.addChild(body); materialContainer.addChild(border); materialContainer.addChild(rotate); materialContainer.addChild(close); materialContainer.addChild(scale); materialContainer.position.set(100, 100); // 设置位置 materialContainer.pivot.set(body.width / 2, body.height / 2); app.stage.addChild(materialContainer); //materialContainer绑定事件 //..... //..... function onDragStart(event) { if (!this.dragging) { var oClickStagePosition = this.data.getLocalPosition(this.parent) var angle = this.rotation / 0.78 * (-45) * (2 * Math.PI / 360); //因为有旋转功能 所以要把旋转后角度计算进去 var x = oClickStagePosition.x - this.position.x; var y = oClickStagePosition.y - this.position.y; var x1 = x * Math.cos(angle) - y * Math.sin(angle); var y1 = y * Math.cos(angle) + x * Math.sin(angle); this.pivot.set(this.children[0].width / 2 + x1, this.children[0].height / 2 + y1); this.position.x = this.position.x + x; this.position.y = this.position.y + y; } } function onDragEnd() { if (this.dragging) { var oClickStagePosition = this.data.getLocalPosition(this.parent); this.position.x = oClickStagePosition.x; this.position.y = oClickStagePosition.y; } } function onDragMove() { if (this.dragging) { var angle = this.rotation / 0.78 * (45) * (2 * Math.PI / 360); var x1 = (this.pivot.x - this.children[0].width / 2) * Math.cos(angle) - (this.pivot.y - this.children[0].height / 2) * Math.sin(angle); var y1 = (this.pivot.y - this.children[0].height / 2) * Math.cos(angle) + (this.pivot.x - this.children[0].width / 2) * Math.sin(angle); this.position.x = this.position.x - x1; this.position.y = this.position.y - y1; this.pivot.set(this.children[0].width / 2, this.children[0].height / 2); this.dragging = false; this.data = null; } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230220.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月11日 下午4:01
下一篇 2026年3月11日 下午4:22


相关推荐

  • TTL门电路与CMOS门电路

    TTL门电路与CMOS门电路TTL 和 CMOS 门电路的区别 nbsp nbsp nbsp nbsp 1 TTL 和带缓冲的 TTL 信号输出高电平 2 4V 输出低电平 2 0V 输入低电平 nbsp nbsp nbsp nbsp 2 CMOS 电平 nbsp nbsp nbsp nbsp nbsp 1 逻辑电平电压接近于电源电压 0 逻辑电平接近于 0V 而且具有很宽的噪声容限 nbsp nbsp nbsp nbsp nbsp 3 电平转换电路 nbsp nbsp nbsp nbsp nbsp 因为 TTL 和 COMS 的高低电平的值不一样 ttl5vcmos3 3v

    2026年3月19日
    4
  • idea2021.5永久激活码(最新序列号破解)

    idea2021.5永久激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    90
  • 关于namecheap 域名运营商,域名赎回详细步骤

    关于namecheap 域名运营商,域名赎回详细步骤

    2022年2月17日
    83
  • 三角形面积的计算公式小学_正三角形面积公式

    三角形面积的计算公式小学_正三角形面积公式1、三角形面积=1/2*底*高(三边都可做底)2、三角形面积=1/2absinC=1/2acsinB=1/2bcsinA3、三角形面积=abc/4R(其中R是三角形外接圆半径)你看看理解一下,其中1是比较常用的。4、三角形面积S=√x*(x-a)*(x-b)*(x-c)其中"√"是大根号,”x”为三角形周长的一半,a,b,c为边长三角形的面积的平方=p(p-a)(p-b)(p-

    2025年6月3日
    4
  • 软件測试自学指南—从入门到精通

    软件測试自学指南—从入门到精通

    2021年11月23日
    37
  • Andon系统设计总结

    Andon系统设计总结一个简简单单的安灯系统 居然话费了如此久的时间 大概两个月 让我十分汗颜 也十分的羞愧 然而 事情已经发生 人生没有 ctrl Z 所以总结经验教训是最主要的 1 需求不明确 用户的需求提的比较模糊 那么作为软件设计及实施人员 应该深入的思考 并就用户提出的初步需求和方案进行分析 并针对性的提出问题或者建议 并得出双方都认可的一致性的结论 2 解决方式不全面 同样的需求 可以有多种的解决方式

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号