可以使用Kinetic.Tween來擴展您的fillPattern:
tween = new Kinetic.Tween({
node:image,
fillPatternScaleX:(1+scaleFactor*2),
fillPatternScaleY:(1+scaleFactor*2),
fillPatternOffsetX:0,
fillPatternOffsetY:0,
duration: .25,
onFinish:function(){ this.reverse(); }
});
tween.play();
這裏是代碼和一個小提琴:http://jsfiddle.net/m1erickson/NHZAV/
<!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 tween;
var scaleFactor=.25;
var img=new Image();
img.onload=function(){
var image=new Kinetic.Rect({
x:50,
y:50,
width:img.width*(1+scaleFactor*2),
height:img.height*(1+scaleFactor*2),
stroke:"red",
strokeWidth:3,
fillPatternImage:img,
fillPatternRepeat:"no-repeat",
fillPatternOffsetX:-img.width*scaleFactor,
fillPatternOffsetY:-img.height*scaleFactor,
});
layer.add(image);
layer.draw();
tween = new Kinetic.Tween({
node:image,
fillPatternScaleX:(1+scaleFactor*2),
fillPatternScaleY:(1+scaleFactor*2),
fillPatternOffsetX:0,
fillPatternOffsetY:0,
duration: .25,
onFinish:function(){ this.reverse(); }
});
tween.play();
}
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/ship.png";
$("#myButton").click(function(){
tween.play();
});
}); // end $(function(){});
</script>
</head>
<body>
<button id="myButton">Again</button>
<div id="container"></div>
</body>
</html>ml>
真棒!很確定這適合我的需求。非常感謝! – Brian