如何获取画布中图像元素的坐标
How to get Co-ordinates of image element in canvas
我正在尝试将图像拖放到画布中,并在画布中获得该图像的完美坐标。但每当我在画布上移动同一个图像时,我都不会得到该图像的新坐标。当我移动图像时,我想帮助确定它的新坐标。
这是我的代码:
// 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="https://dl.dropboxusercontent.com/u/139992952/multple/4top.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();
}
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 type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<h4>Drag from toolbar onto canvas. Then drag around canvas.</h4>
<div id="toolbar">
<img id="house" width=32 height=32 src="https://dl.dropboxusercontent.com/u/139992952/multple/4top.png"><br>
</div>
<div id="container"></div>
每个KineticJS对象,就像图像对象一样,都有保持其当前位置的x,y属性。
在最新版本的KineticJS中,您可以使用获取对象的x,y
var x=myImageObject.x();
var y=myImageObject.y();
或者你可以获取一个包含当前x,y的对象,如下所示:
// myXY has x,y properties: myXY.x and myXY.y
var myXY=myImageObject.position();
相关文章:
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- 查找仅适用于原始图像的图像放大算法的名称
- 全屏背景图像+旋转图像
- 无法使用javascript插件在首次运行时加载实际图像大小(图像在刷新后加载)
- 用Javascript替换图像和图像标题/alt文本
- 如何在上传前在浏览器中去除图像元数据(javascript)
- 无法设置图像背景图像
- 单击图像更改图像并调用函数
- 使用javascript只滚动一个图像进行图像滚动
- 如何使用相同大小的Lazy-Load图像修复图像环绕大小
- 使用javascript中的if和else语句来显示图像(如果图像不存在,则显示其他图像)
- 响应图像tinymce图像链接
- Javascript-根据所选图像更改图像
- 显示从文件选择器中选择的图像的图像缩略图
- jquery函数,用于获取预览图像的图像
- 如何更改多个图像的图像src
- 学习JavaScript:有没有办法在不使用个人ID的情况下更改一组图像的图像文件
- 从HTML5 FileApi加载的图像中检索EXIF图像元数据
- 无法使用exif获取图像元数据的属性
- 使用JavaScript编写图像元数据