javascript - 使用画布显示的类似图像
问题描述
我正在尝试在画布上显示多个图像。画布上最近添加的图像必须是可拖动的。这是 HTML 代码。
<canvas id="panoramaCanvas" width=1500 height=1000></canvas>
我正在使用一个对象来跟踪已添加的图像。每次我在画布上拖动 Image 时,都会调用此方法。storeData 存储当前添加的图像数据。
var storeData = []; // Object
var panoramaImg = new Image();
var imgNumber = 0;
function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
//
var url = e.dataTransfer.getData('text/plain');
// for img elements, url is the img src so
// create an Image Object & draw to canvas
if (url) {
panoramaImg.onload = function () {
panoramaCtx.globalAlpha = 0.4;
panoramaCtx.drawImage(panoramaImg, 0, 0, this.width, this.height, 0, 0, 500, 500);
panoramaCtx.globalAlpha = 1.0;
storeData.push({
imgData: panoramaImg,
imgNo: imgNumber + 1,
xPoint: 0,
yPoint: 0,
});
imgNumber = imgNumber + 1;
}
//img.width, img.height, 0, 0, rangeWidth.value, rangeHeight.value
panoramaImg.src = url;
// for img file(s), read the file & draw to canvas
} else {
handleFiles(e.dataTransfer.files);
}
}
在鼠标事件上调用此方法。
var canvasOffset1 = $("#panoramaCanvas").offset();
var offsetX1 = canvasOffset1.left;
var offsetY1 = canvasOffset1.top;
var canvasWidth = panoramaCanvas.width;
var canvasHeight = panoramaCanvas.height;
var isDragging = false;
function handleMouseDownEvent(e) {
canMouseX = parseInt(e.clientX - offsetX1);
canMouseY = parseInt(e.clientY - offsetY1);
// set the drag flag
isDragging = true;
}
function handleMouseUpEvent(e) {
canMouseX = parseInt(e.clientX - offsetX1);
canMouseY = parseInt(e.clientY - offsetY1);
// clear the drag flag
isDragging = false;
}
function handleMouseOutEvent(e) {
canMouseX = parseInt(e.clientX - offsetX1);
canMouseY = parseInt(e.clientY - offsetY1);
// user has left the canvas, so clear the drag flag
//isDragging=false;
}
function handleMouseMoveEvent(e) {
canMouseX = parseInt(e.clientX - offsetX1);
canMouseY = parseInt(e.clientY - offsetY1);
// if the drag flag is set, clear the canvas and draw the image
if (isDragging) {
panoramaCtx.clearRect(0, 0, canvasWidth, canvasHeight);
panoramaCtx.globalAlpha = 0.4;
if (storeData.length > 1)
{
for (var i = 0; i <= storeData.length - 2; i++) {
panoramaCtx.drawImage(storeData[i].imgData, storeData[i].xPoint, storeData[i].yPoint,
500, 500);
}
}
panoramaCtx.drawImage(panoramaImg, canMouseX - 1128 / 2, canMouseY - 120 / 2, 500, 500);
panoramaCtx.globalAlpha = 1.0;
storeData[imgNumber - 1].xPoint = canMouseX - 1128 / 2;
storeData[imgNumber - 1].yPoint = canMouseY - 120 / 2;
console.log(storeData);
}
}
当我添加第一张图像时,鼠标事件一切正常,但是当我添加第二张图像时,图像数据首先更改为第二张图像的图像数据。
当我试图改变第二张图像的位置和第一张图像的图像数据发生变化时。
显示图像时我在哪里做错了?
解决方案
您正在创建一个HTMLImageElement panoramaImg
。因此,所有成员的imgData
属性都是相同的,并且只有HTMLImageElement指向最后放置的图像。storeData
只需在您的放置处理程序中创建一个new Image()
,而不是重复使用它。
推荐阅读
- hive - 如何在hive中合并两个表
- reactjs - 一般来说,在单个组件中使用一个或多个 useEffect 挂钩更好吗?
- php - PHP 脚本在哪个用户下运行?
- angularjs - 如何将对象传递给angularjs中组件的模板函数
- apache-kafka - Kafka主题的Nifi路由
- sql - SQL Server 查询以更新具有相同名称的多行
- string - 如何从 Neo4j 密码中的列表字符串中获取值
- angular - fromEvent 没有得到键盘事件
- java - 显示来自特定用户的数据(android studio java 编程 + firebase)
- ios - 来自 ios 应用程序的谷歌地图中的源按钮