gpt4 book ai didi

javascript - 将图像拖放到 Canvas 上后允许拖动

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:39 25 4
gpt4 key购买 nike

$(function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// get the offset position of the container
var $canvas = $("#canvas");
var Offset = $canvas.offset();
var offsetX = Offset.left;
var offsetY = Offset.top;

// select all .tool's
var $tools = $(".tool");

// make all .tool's draggable
$tools.draggable({
helper: 'clone',
revert: 'invalid'
});


// assign each .tool its index in $tools
$tools.each(function (index, element) {
$(this).data("toolsIndex", index);
});

// make the canvas a dropzone
$canvas.droppable({
drop: dragDrop,
});

// handle a drop into the canvas
function dragDrop(e, ui) {

// get the drop point (be sure to adjust for border)
var x = parseInt(ui.offset.left - offsetX);
var y = parseInt(ui.offset.top - offsetY);

// get the drop payload (here the payload is the $tools index)
var theIndex = ui.draggable.data("toolsIndex");

// drawImage at the drop point using the dropped image
ctx.drawImage($tools[theIndex], x, y, 32, 32);

}
});

我尝试了很多事情,但都失败了。此代码允许我将多个图像拖放到 Canvas 元素上。我需要做的是添加在图像被放下后再次拖动图像的可能性。我知道每次都必须重新绘制 Canvas ,但我不知道如何。

谁能帮我解决这个问题?

最佳答案

由于您评论说您对 Canvas 库持开放态度,下面是一个示例,您可以:

  • 使用 jqueryUI 从 toolbar-div 中拖动一个 img 元素。
  • 将 img 放到 Canvas 上并创建一个 KineticJS.Image 对象,您可以在 Canvas 上拖动它。

演示:http://jsfiddle.net/m1erickson/gkefk/

结果:一个 img 从蓝色工具栏拖动 3X,放到灰色 Canvas 上,然后在 Canvas 上拖动。

result screenshot

这是一个带注释的代码示例:

$(function() {

// get a reference to the house icon in the toolbar
// hide the icon until its image has loaded
var $house = $("#house");
$house.hide();



// get the offset position of the kinetic container
var $stageContainer = $("#container");
var stageOffset = $stageContainer.offset();
var offsetX = stageOffset.left;
var offsetY = stageOffset.top;

// create the Kinetic.Stage and layer
var stage = new Kinetic.Stage({
container: 'container',
width: 350,
height: 350
});
var layer = new Kinetic.Layer();
stage.add(layer);

// start loading the image used in the draggable toolbar element
// this image will be used in a new Kinetic.Image
var image1 = new Image();
image1.onload = function() {
$house.show();
}
image1.src = "/image/GeibZ.png";

// make the toolbar image draggable
$house.draggable({
helper: 'clone',
});

// set the data payload
$house.data("url", "house.png"); // key-value pair
$house.data("width", "32"); // key-value pair
$house.data("height", "33"); // key-value pair
$house.data("image", image1); // key-value pair

// make the Kinetic Container a dropzone
$stageContainer.droppable({
drop: dragDrop,
});

// handle a drop into the Kinetic container
function dragDrop(e, ui) {

// get the drop point
var x = parseInt(ui.offset.left - offsetX);
var y = parseInt(ui.offset.top - offsetY);

// get the drop payload (here the payload is the image)
var element = ui.draggable;
var data = element.data("url");
var theImage = element.data("image");

// create a new Kinetic.Image at the drop point
// be sure to adjust for any border width (here border==1)
var image = new Kinetic.Image({
name: data,
x: x,
y: y,
image: theImage,
draggable: true
});
layer.add(image);
layer.draw();
}

}); // end $(function(){});
body {
padding: 20px;
}

#container {
border: solid 1px #ccc;
margin-top: 10px;
width: 350px;
height: 350px;
}

#toolbar {
width: 350px;
height: 35px;
border: solid 1px blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/kineticjs/4.7.2/kinetic.min.js"></script>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Prototype</title>
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
</head>

<body>
<div id="toolbar">
<img id="house" width=32 height=32 src="/image/GeibZ.png"><br>
</div>
<div id="container"></div>
</body>

</html>

关于javascript - 将图像拖放到 Canvas 上后允许拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22375971/

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