惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
罗磊的独立博客
博客园 - 聂微东
T
Troy Hunt's Blog
美团技术团队
IT之家
IT之家
A
Arctic Wolf
腾讯CDC
雷峰网
雷峰网
SecWiki News
SecWiki News
博客园_首页
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
量子位
N
News and Events Feed by Topic
小众软件
小众软件
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
J
Java Code Geeks
V
V2EX
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
Webroot Blog
Webroot Blog
F
Fortinet All Blogs
P
Privacy International News Feed
NISL@THU
NISL@THU
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
PCI Perspectives
PCI Perspectives
GbyAI
GbyAI
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
V
Visual Studio Blog

博客园 - 浊浊然

kkfileview5 安装 css毛玻璃效果 createjs 实现场景拖动、滚轮放大缩小 node-sass 安装不上的问题 nodejs 搭建本地服务器 微信小程序,真机上接口调不通 mysql 去除字段中 空格 制表符等 用jeesite做后台开发,新写了个前端业务系统的权限问题 uniapp 打包app 用到地图相关 升级node最新版本18.x .Error: error:0308010C js 读取url中参数值 linux php 安装oci8 electron nativefier打包网址 electron-winstaller制作安装包 h5+ 上传图片(选择图片、拍照) phpword 导出word,文件已损坏问题 Vscode 右键 open with code 没有的情况,使用以下注册表脚本 PhpSpreadsheet导出excel apache https小程序android ssl error js获取url参数,以及中文乱码问题 微信js上传图片并 展示,iphone下预览
createjs 实现图片拖动
浊浊然 · 2025-09-04 · via 博客园 - 浊浊然

背景是一张大图,显示区域窗口小时,需要拖动查看,游戏地图的简单实现

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <title>背景拖动</title>
 7     <script src="https://code.createjs.com/1.0.0/easeljs.min.js"></script>
 8     <style>
 9         body { margin: 0; overflow: hidden; background-color: #222; }
10         canvas { display: block; }
11         #gameContainer { position: relative; }
12     </style>
13 </head>
14 <body>
15     <div id="gameContainer">
16         <canvas id="gameCanvas" width="200" height="200"></canvas>
17     </div>
18     <script>
19         // 获取 canvas 元素
20 var canvas = document.getElementById("gameCanvas");
21 var stage = new createjs.Stage(canvas);
22 
23 // 加载地图图像
24 var mapImage = new Image();
25 mapImage.src = './bg.jpeg'; // 替换为你的地图图片路径
26 mapImage.onload = function() {
27     var mapBitmap = new createjs.Bitmap(mapImage);
28 
29     // 设置地图位置为右下角
30     // mapBitmap.x = canvas.width - mapImage.width;
31     // mapBitmap.y = canvas.height - mapImage.height;
32 
33     stage.addChild(mapBitmap);
34     stage.update(); // 更新舞台以显示图像
35 
36     var isDragging = false;
37     var lastX, lastY;
38 
39     mapBitmap.on("mousedown", function(event) {
40         isDragging = true;
41         lastX = event.stageX;
42         lastY = event.stageY;
43     });
44     mapBitmap.on("pressmove", function(event) {
45         // console.log(isDragging,event.stageX, event.stageY);
46         if (isDragging) {
47             var dx = event.stageX - lastX;
48             var dy = event.stageY - lastY;
49             // 检查边界并更新位置
50             var newX = mapBitmap.x + dx;
51             var newY = mapBitmap.y + dy;
52             if (newX <= 0 && newX >= (canvas.width - mapImage.width)) { // 检查 X 轴边界
53                 mapBitmap.x = newX;
54             }
55             if (newY <= 0 && newY >= (canvas.height - mapImage.height)) { // 检查 Y 轴边界
56                 mapBitmap.y = newY;
57             }
58             // mapBitmap.x = newX;
59             // mapBitmap.y = newY;
60             // console.log(mapBitmap.x, mapBitmap.y)
61             lastX = event.stageX;
62             lastY = event.stageY;
63             stage.update(); // 更新舞台以显示新的位置(如果位置有变化)
64         }
65     });
66 
67     mapBitmap.on("pressup", function(event) {
68         isDragging = false; // 停止拖动
69     });
70 };
71 
72     </script>
73 </body>
74 </html>