我正在尝试移动图像,但我不能
...
var imageObj = new Image();
imageObj.onload = function() {
var yoda = new Kinetic.Image({
x: 200,
y: 50,
image: imageObj,
width: 106,
height: 118
});
....
// event button - move image;
document.getElementById('show').addEventListener('click', function() {
imageObj.move(0,5);
}, false);
答案 0 :(得分:0)
一些错误:
一定要绘制图层,以便显示更改
你要移动的元素是yoda,而不是imageObj:yoda.move(0,5)
yoda对象必须在imageObj.onload之外声明,以便稍后在范围内
这是代码和小提琴:http://jsfiddle.net/m1erickson/T2pfZ/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<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>
<style>
body{padding:20px;}
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:350px;
height:350px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 350,
height: 350
});
var layer = new Kinetic.Layer();
stage.add(layer);
var yoda;
var imageObj = new Image();
imageObj.onload = function() {
yoda = new Kinetic.Image({
x: 200,
y: 50,
image: imageObj,
width: 106,
height: 118
});
layer.add(yoda);
layer.draw();
}
imageObj.src="houseicon.png";
document.getElementById('show').addEventListener('click', function() {
yoda.move(0,5);
layer.draw();
}, false);
}); // end $(function(){});
</script>
</head>
<body>
<button id="show">Move!</button>
<div id="container"></div>
</body>
</html>