代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>绘制矩形</title>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div>
<button @click="zoom+=0.5">放大</button>
<button @click="zoom-=0.2">缩小</button>
{{zoom}}
<button @click="divList=[]">清除画布</button>
</div>
<div
class="box"
:style="{'zoom':zoom}"
style="
width: 10000px;
height: 10000px;
border: 1px solid #000;
overflow: auto;
"
@click="onclickHandle"
@mousedown="onmousedownHandle"
@mouseup="onmouseupHandle"
@mousemove="onmousemoveHandle"
>
<div
v-for="(item,index) in divList"
:style="{width:'9px',height:'9px',left:(item.column*10+1)+'px',top:(item.row*10+1)+'px'}"
class="fk"
></div>
</div>
</div>
<script>
new Vue({
el: "#app",
data: {
zoom: 1,
leftDown: false,
divList: [
{ row: 5, column: 5 },
{ row: 1, column: 4 },
],
},
methods: {
onclickHandle(e) {
var box = document.getElementsByClassName("box")[0];
var x = e.clientX - 30 + document.documentElement.scrollLeft;
var y = e.clientY - 52 + document.documentElement.scrollTop;
console.log(x, y, box.offsetLeft, box.offsetTop);
this.insertCell(x, y, "点击");
},
onmousedownHandle(e) {
this.leftDown = true;
},
onmouseupHandle(e) {
this.leftDown = false;
},
onmousemoveHandle(e) {
if (this.leftDown) {
var box = document.getElementsByClassName("box")[0];
var x = e.clientX - 30 + document.documentElement.scrollLeft;
var y = e.clientY - 52 + document.documentElement.scrollTop;
// console.log("移动1", e, x, y);
this.insertCell(x, y, "移动2");
}
},
insertCell(x, y, type) {
if (x <= 0 || y <= 0) return;
var _x = Math.floor(x / (10 * this.zoom)),
_y = Math.floor(y / (10 * this.zoom));
if (_x < 0 || _y < 0) return;
console.log(type, x, y, _x, _y);
var cell = this.divList.filter(
(it) => it.row == _y && it.column == _x
);
if (cell.length > 0) {
return;
}
this.divList.push({
row: _y,
column: _x,
});
console.log("数量", this.divList.length);
},
},
});
</script>
</body>
<style>
* {
margin: 0;
padding: 0;
}
#app {
margin: 30px;
}
.box {
position: relative;
background-size: 10px 10px, 10px 10px;
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 1px, transparent 0),
linear-gradient(90deg, rgba(0, 0, 0, 0.1) 1px, transparent 0);
}
.fk {
background-color: #000;
position: absolute;
}
</style>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。