我有这段代码,它绘制一个圆角十六进制,并在鼠标悬停时放大和缩小。如何使代码模块化和可重用,如何将其变成函数?特别是十六进制绘图代码?
<script src="js/kinetic.min.js" charset="utf-8"></script>
<script defer="defer" type="text/javascript">
function zoomHex() {
}
var stage = new Kinetic.Stage({
container: 'container',
width: 1024,
height: 800
});
var shapesLayer = new Kinetic.Layer();
var hex = new Kinetic.Shape({
x: 5,
y: 10,
fill: 'rgb(53, 74, 159)',
// a Kinetic.Canvas renderer is passed into the drawFunc function
drawFunc: function(canvas) {
var context = canvas.getContext();
context.beginPath();
context.moveTo(0.1, 51.9);
context.bezierCurveTo(0.1, 43.6, 4.6, 35.8, 11.9, 31.6);
context.lineTo(61.0, 3.3);
context.bezierCurveTo(68.2, -0.9, 77.2, -0.9, 84.4, 3.3);
context.lineTo(133.6, 31.6);
context.bezierCurveTo(140.8, 35.8, 145.3, 43.6, 145.3, 52.0);
context.lineTo(145.3, 108.7);
context.bezierCurveTo(145.3, 117.1, 140.8, 124.8, 133.6, 129.0);
context.lineTo(84.4, 157.4);
context.bezierCurveTo(77.2, 161.5, 68.2, 161.5, 61.0, 157.4);
context.lineTo(11.9, 129.0);
context.bezierCurveTo(4.6, 124.8, 0.1, 117.0, 0.1, 108.7);
context.lineTo(0.1, 51.9);
context.closePath();
canvas.fillStroke(this);
}
});
var zoomIn = new Kinetic.Animation(function(frame) {
var period = 2500;
var duration = 250;
zoomAmount = 1;
var scale =frame.time / period;
hex.setScale(frame.time / period + zoomAmount);
if(frame.time > duration) {
zoomIn.stop();
this.frame.time = 0;
}
}, shapesLayer);
var zoomOut = new Kinetic.Animation(function(frame) {
var period = 2500;
var duration = 250;
zoomAmount = 1;
hex.setScale(zoomAmount - frame.time / period + 0.102);
if(frame.time > duration) {
zoomOut.stop();
this.frame.time = 0;
}
}, shapesLayer);
hex.on('mouseover', function() {
zoomIn.start();
//zoomIn.stop();
});
hex.on('mouseleave', function() {
zoomOut.start();
//zoomOut.stop();
});
shapesLayer.add(hex);
stage.add(shapesLayer);
</script>