<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.main {
width: 902px;
border: 1px solid #000000;
}
</style>
</head>
<body>
<div class="main">
<canvas class="canvas" id="myCanvas" width="900" height="600"></canvas>
</div>
</body>
<script type="text/javascript">
let roomNum = 1,
moveX = 0,
moveY = 0,
yuanX = 0,
yuanY = 0,
endX = 0,
endY = 0
roomNum = 900 / 450;
let isDraging = false; // 记录元素是否可以拖动
let canvas = document.getElementById('myCanvas');
let ctx = canvas.getContext('2d')
makeTxxx(roomNum, moveX, moveY);
canvas.addEventListener('mousewheel', (e) => {
zoom(e)
})
canvas.addEventListener('mousemove', (e) => {
move(e)
})
// 鼠标点击
canvas.addEventListener('mousedown', (e) => {
var e = e || window.event;
// 鼠标距离div左侧偏移距离 = e.pageX鼠标距离页面左侧距离 - get('move').offsetLeft为div距离页面左侧距离
yuanX = e.pageX;
yuanY = e.pageY;
isDraging = true;
})
canvas.addEventListener('mouseup', (e) => {
isDraging = false;
let x = (e.pageX - yuanX);
let y = (e.pageY - yuanY);
moveX += x;
moveY += y;
})
function zoom(e) {
let z = e.deltaY > 0 ? -0.02 : 0.02
roomNum += z;
makeTxxx(roomNum, moveX, moveY);
}
function move(e) {
if (isDraging === true) {
let x = (e.pageX - yuanX) + moveX;
let y = (e.pageY - yuanY) + moveY;
makeTxxx(roomNum, x, y);
}
}
function makeTxxx(roomNum, moX, moY) {
ctx.clearRect(0, 0, 900, 600);
// 坐标轴Y
hzText("Y轴", 10, 100)
drawDashedLine(20, 20, 20, 200, 0);
drawDashedLine(10, 180, 20, 200, 0);
// 坐标轴X
hzText("X轴", 100, 20)
drawDashedLine(20, 20, 200, 20, 0);
drawDashedLine(180, 10, 200, 20, 0);
ctx.beginPath()
let rateX = roomNum + moX;
let rateY = roomNum + moY;
ctx.fillRect(50 * roomNum + moX, 70 * roomNum + moY, 350 * roomNum, 50 * roomNum);
// X 轴
drawDashedLine(50 * roomNum + moX, 130 * roomNum + moY, 400 * roomNum + moX, 130 * roomNum + moY, 2);
drawDashedLine(50 * roomNum + moX, 120 * roomNum + moY, 50 * roomNum + moX, 140 * roomNum + moY, 0);
drawDashedLine(400 * roomNum + moX, 120 * roomNum + moY, 400 * roomNum + moX, 140 * roomNum + moY, 0);
hzText("350mm", 200 * roomNum + moX, 128 * roomNum + moY)
// Y轴
drawDashedLine(40 * roomNum + moX, 70 * roomNum + moY, 40 * roomNum + moX, 120 * roomNum + moY, 2);
drawDashedLine(30 * roomNum + moX, 70 * roomNum + moY, 50 * roomNum + moX, 70 * roomNum + moY, 0);
drawDashedLine(30 * roomNum + moX, 120 * roomNum + moY, 50 * roomNum + moX, 120 * roomNum + moY, 0);
hzText("50mm", 35 * roomNum + moX, 95 * roomNum + moY)
ctx.stroke()
// ctx.beginPath()
// ctx.fillRect(50 * roomNum + moX, 150 * roomNum + moY, 350 * roomNum, 50 * roomNum);
// ctx.stroke()
}
function drawDashedLine(x1, y1, x2, y2, dashLength = 2) {
let deltaX = x2 - x1;
let deltaY = y2 - y1;
// 生成虚线段的路径
ctx.beginPath();
if (dashLength > 0) {
// 求虚线段的个数
let dashNums = Math.floor(
Math.sqrt(deltaX * deltaX + deltaY * deltaY) / dashLength
);
for (let i = 0; i < dashNums; i++) {
let nextX = x1 + (deltaX / dashNums) * i;
let nextY = y1 + (deltaY / dashNums) * i;
if (i % 2 === 0) {
ctx.moveTo(nextX, nextY);
} else {
ctx.lineTo(nextX, nextY);
}
}
} else {
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
// ctx.fillStyle = "#d40007" // 填充颜色
ctx.strokeStyle = '#130603';
}
// 绘制虚线段
ctx.stroke();
}
function hzText(txt, x, y) {
ctx.beginPath();
const {
width
} = ctx.measureText('背景A');
ctx.fillText(txt, x, y);
ctx.textBaseline = 'bottom';
ctx.stroke();
}
</script>
</html>
canvas实现鼠标拖动缩放
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、拖动所要的鼠标事件mousedown(鼠标按下)、mousemove(鼠标移动)及mouseup(鼠标抬起)1...
- // 如果为真,则允许用户旋转相机。如果为假,相机将锁定到当前标题。此标志仅适用于2D和3D。(可以理解为禁用鼠标...
- 需求 使用vue实现滑动拼图验证码 踩到的坑 使用@mousemove绑定事件拖拽速度过快有严重的卡顿 源代码 解...
- 需求如下: 1、在一个页面中显示两张图片 2、进入页面可以使用鼠标拖动各自的图片,相互不受影响 3、进入页面后可以...