0

我正在尝试使用以下方法将图像放置在一个圆圈内:

        var gArrow2 = new Image();
        gArrow2.src = '../../Content/images/green_s.png';
        var circle2 = new Kinetic.Circle({
            drawFunc: function(canvas) {
              var context = canvas.getContext();
              context.drawImage(gArrow2, 256, 256, 11, 23);
              context.beginPath();
              context.arc(256, 256, this.getRadius(), 0, 2 * Math.PI, false);
              context.lineWidth = this.getStrokeWidth();
              context.strokeStyle = this.getStroke();
              context.stroke();
            },
            x: 256,
            y: 256,
            radius: 70,
            stroke: '#00ffff',
            strokeWidth: 4,
            opacity: 0.5
        });
        layer2.add(circle2);

这没用!我会很感激你的建议,在此先感谢。

4

1 回答 1

0

你必须给 gArrow2 时间来加载。您可以通过 gArrow2.onload 执行此操作。

始终在加载后放置 gArrow2.src 。

然后将任何使用 gArrow2 的代码放入onload函数中。

[编辑添加 layer2.draw()]

我插入你的代码并让它工作。

看起来只需要 layer2.draw();

这是代码和小提琴:http: //jsfiddle.net/m1erickson/Qgnkx/

<!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://www.html5canvastutorials.com/libraries/kinetic-v4.3.3-beta.js"></script>

<style>
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
}
</style>        
<script>
$(function(){

        var stage = new Kinetic.Stage({
            container: 'container',
            width: 600,
            height: 600
        });
        var layer2 = new Kinetic.Layer();
        stage.add(layer2);


        var gArrow2 = new Image();
        gArrow2.onload=function(){

            var circle2 = new Kinetic.Circle({
                drawFunc: function(canvas) {
                  var context = canvas.getContext();
                  context.drawImage(gArrow2, 256, 256, 11, 23);
                  context.beginPath();
                  context.arc(256, 256, this.getRadius(), 0, 2 * Math.PI, false);
                  context.lineWidth = this.getStrokeWidth();
                  context.strokeStyle = this.getStroke();
                  context.stroke();
                },
                x: 256,
                y: 256,
                radius: 70,
                stroke: '#00ffff',
                strokeWidth: 4,
                opacity: 0.5
            });
            layer2.add(circle2);        
            layer2.draw();
        }
        gArrow2.src = 'http://dl.dropbox.com/u/139992952/stackoverflow/house1.jpg';


}); // end $(function(){});

</script>       
</head>

<body>
    <div id="container"></div>
</body>
</html>
于 2013-03-20T19:59:19.677 回答