-1
我是javascript程序員的初學者,我無法將圖像添加到畫布 這條線會連接兩個已被點擊的圖像?我很困惑如何根據所需畫布的座標添加圖像?我可以使用函數kineticjs添加圖像並組織座標嗎?請幫我 這是一個HTML代碼添加畫布和畫線如何在點擊2張圖片後繪製線條
<html>
<head>
<style>
canvas {
border: 1px dashed rgb(200, 200, 200);
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<script src = "kinetic-v4.5.4.min.js"></script>
<script defer = "defer">
var clicks = 0;
var lastClick = [0, 0];
document.getElementById('canvas').addEventListener('click', drawLine, false);
function getCursorPosition(e) {
var x;
var y;
if (e.pageX != undefined && e.pageY != undefined) {
x = e.pageX;
y = e.pageY;
} else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
return [x, y];
}
function drawLine(e) {
context = this.getContext('2d');
x = getCursorPosition(e)[0] - this.offsetLeft;
y = getCursorPosition(e)[1] - this.offsetTop;
if (clicks != 1) {
clicks++;
} else {
context.beginPath();
context.moveTo(lastClick[0], lastClick[1]);
context.lineTo(x, y, 6);
context.strokeStyle = '#000000';
context.stroke();
clicks = 0;
}
lastClick = [x, y];
};
</script>
</body>
</html>
謝謝你......我會試試看 – PutriKinara