如果使用 KineticJS 绘制圆弧,在浏览器控制台中会出现错误:“Object # has no method 'getContext'” 代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var myarc = new Kinetic.Shape({
drawFunc: function(canvas) {
var context=canvas.getContext('2d');
var x = stage.getWidth()/2;
var y = stage.getHeight()/2;
var radius = 70;
var startAngle = 1 * Math.PI;
var endAngle = 0 * Math.PI;
context.beginPath();
context.arc(x, y, radius, startAngle, endAngle, false);
context.stroke(this);
canvas.fillStroke(this);
},
fill: '#00D2FF',
stroke: 'red',
strokeWidth: 15,
draggable:true
});
myarc.on('mouseover', function() {
this.setStroke('blue');
this.setStrokeWidth(20);
layer.draw();
});
myarc.on('mouseout', function() {
this.setStroke('black');
this.setStrokeWidth(4);
layer.draw();
});
// add the shape to the layer
layer.add(myarc);
// add the layer to the stage
stage.add(layer);
</script>
<div id="container"></div>
</body>
</html>
我可以在不使用画布的情况下绘制弧线,只需将“上下文”作为 drawFunc 函数参数传输:
drawFunc: function(context) {
,但在这种情况下,我无法在鼠标悬停事件中更改弧的宽度。