gpt4 book ai didi

javascript - 用vue在canvas上画图

转载 作者:行者123 更新时间:2023-12-02 21:45:23 24 4
gpt4 key购买 nike

我想在 VueJs 中使用矩形(用 JS 和 html 制作)调整选择。我有这个版本: https://codepen.io/sebastiancz/pen/mdJVJRw

initDraw(document.getElementById('canvas'));

function initDraw(canvas) {

function setMousePosition(e) {
var ev = e || window.event; //Moz || IE
if (ev.pageX) { //Moz
mouse.x = ev.pageX + window.pageXOffset;
mouse.y = ev.pageY + window.pageYOffset;
} else if (ev.clientX) { //IE
mouse.x = ev.clientX + document.body.scrollLeft;
mouse.y = ev.clientY + document.body.scrollTop;
}
};

var mouse = {
x: 0,
y: 0,
startX: 0,
startY: 0
};
var element = null;

canvas.onmousemove = function (e) {
setMousePosition(e);
if (element !== null) {
element.style.width = Math.abs(mouse.x - mouse.startX) + 'px';
element.style.height = Math.abs(mouse.y - mouse.startY) + 'px';
element.style.left = (mouse.x - mouse.startX < 0) ? mouse.x + 'px' : mouse.startX + 'px';
console.log(mouse.x, mouse.y)
element.style.top = (mouse.y - mouse.startY < 0) ? mouse.y + 'px' : mouse.startY + 'px';
}
}

canvas.onmousedown = function (e) {

console.log("Start.");
mouse.startX = mouse.x;
mouse.startY = mouse.y;
element = document.createElement('div');
element.className = 'rectangle'
element.style.left = mouse.x + 'px';
element.style.top = mouse.y + 'px';
canvas.appendChild(element)
}
canvas.onmouseup = function (e) {
element = null;
// canvas.ctx.clearRect();
console.log("finsihed.");

}

}

这是我在 vue 中的非工作版本: https://codepen.io/sebastiancz/pen/mdJPvOP?editors=0011

我该如何解决这个问题?

最佳答案

您可以使用 Vue.js 和 HTML5 Canvas 执行以下操作。您可以通过创建数组并存储选择的开始和结束位置来进一步扩展它以显示以前的选择。

Vue.component("selection", {
template: `<canvas id='canvas' ref='select' @mousedown='startSelect' @mousemove='drawRect' @mouseup='stopSelect'></canvas>`,
data() {
return {
ctx: null,
selectionMode: false,
startPosition: {
x: null,
y: null
}
};
},

methods: {

startSelect(e) {
this.selectionMode = true;
this.startPosition.x = e.clientX;
this.startPosition.y = e.clientY;
},

drawRect(e) {
if (this.selectionMode) {
console.log(this.startPosition);
this.ctx.beginPath();
this.ctx.rect(
this.startPosition.x,
this.startPosition.y,
e.clientX - this.startPosition.x,
e.clientY - this.startPosition.y
);
this.ctx.closePath();
this.ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
this.ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
this.ctx.strokeStyle = "#f00";
this.ctx.stroke();
}
},

stopSelect(e) {
this.ctx.fillStyle = "#fff";

this.selectionMode = false;
this.startPosition.x = null;
this.startPosition.y = null;
}

},
mounted() {
this.$refs.select.height = window.innerHeight;
this.$refs.select.width = window.innerWidth;
this.ctx = this.$refs.select.getContext("2d");
// this.ctx.fillRect(0,0,500,500);
}
});

new Vue({
el: "#app",
data: {
hello: "world"
}
});
body {
margin: 2rem;
background: #eee;
}

#canvas {
background: white;
box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.2);
}
<div id="app">
<selection></selection>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

关于javascript - 用vue在canvas上画图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com