0

我正在尝试使用以下脚本在圆圈内添加图像:

   ....   
   var rCvisible = false; 
    ...   
    var rC = new Image();
             rCircle;
            rC.onload = function () {
                   rCircle = new Kinetic.Image({
                      image: rC,
                       opacity: 0.3,
                      visible: rCvisible
                   });
              };


         rC.src = '../../Content/images/rotate.png';

 var circle2 = new Kinetic.Circle({
                 drawFunc: function (canvas) {
                     var context2 = canvas.getContext();
                     centerX2 = blueLine2.getPosition().x;
                     centerY2 = greenLine2.getPosition().y;
                     context2.drawImage(rC, centerX2 - 20, centerY2 - 20, 44, 40);
                     context2.beginPath();
                     context2.arc(centerX2, centerY2, this.getRadius(), 0, 2 * Math.PI, false);
                     context2.lineWidth = this.getStrokeWidth();
                     context2.strokeStyle = this.getStroke();
                     context2.stroke();
                 },
                 x: cx + gx,
                 y: cy + gy,
                 radius: 70,
                 stroke: '#00ffff',
                 strokeWidth: 3,
                 opacity: 0.5
             });
.....
  circle2.on('mouseover', function () {
                 document.body.style.cursor = 'pointer';
                 rCvisible = true;
                 layer2.draw();
             });
             circle2.on('mouseout', function () {
                 document.body.style.cursor = 'default';
                 rCvisible = false;
                 layer2.draw();
             });

我也试过:

context2.drawImage(rC, centerX2 - 20, centerY2 - 20, 44, 40, setVisible(false));

我的目标是隐藏图像,然后在鼠标在圆圈内时显示它。我收到以下错误:

'setVisible' is undefined

非常感谢您的建议,在此先感谢。

4

1 回答 1

1

您可以使用 fillPatternImage 属性来完全执行您需要的 Fill/NoFill

您可以将任何形状的填充设置为图像而不是颜色。所以,你可以用这样的图像填充你的圈子:

    var circle = new Kinetic.Circle({
      x: 100,
      y: 100,
      radius: 70,
      fillPatternImage: "yourImage",    // this is an Image object  --  new Image();
      fillPatternOffset: [-220, 70],
      stroke: '#00ffff',
      strokeWidth: 3,
      opacity:0.5
    });

您还可以像这样在图像和纯色之间更改形状的填充:

      // change the circle’s fill to solid white
      this.setFill("white");

      // change your circle’s fill to an image
      // Note: you must clear the solid fill before applying the image fill
      this.setFill("");
      this.setFillPatternImage(yourImage);

因此,要获得在鼠标悬停时显示图像的效果,您可以设置以下 Kinetic 事件:

    circle.on('mouseover touchstart', function() {
      this.setFill("");
      this.setFillPatternImage(img);
      this.setFillPatternOffset(-160, 100);
      layer.draw();
    });

    circle.on('mouseout touchend', function() {
      this.setFill("white");
      layer.draw();
    });

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

身体{边距:0px;填充:0px;}

  var img=new Image();
  img.onload=function(){
      draw();
  }
  img.src="http://dl.dropbox.com/u/139992952/stackoverflow/KoolAidMan.png";

  function draw() {

    var stage = new Kinetic.Stage({
      container: 'container',
      width: 200,
      height: 200
    });
    var layer = new Kinetic.Layer();

    var circle = new Kinetic.Circle({
      x: 100,
      y: 100,
      radius: 70,
      fillPatternImage: "",
      fillPatternOffset: [-220, 70],
      stroke: '#00ffff',
      strokeWidth: 3,
      opacity:0.5
    });


    circle.on('mouseover touchstart', function() {
      this.setFill("");
      this.setFillPatternImage(img);
      this.setFillPatternOffset(-160, 100);
      layer.draw();
    });

    circle.on('mouseout touchend', function() {
      this.setFill("white");
      layer.draw();
    });

    layer.add(circle);
    stage.add(layer);
  }

</script>

于 2013-04-01T04:34:26.353 回答