Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>How to draw a wedge containing an image which changes based on clicks/taps</strong></p> <p><img src="https://i.stack.imgur.com/DHEgu.png" alt="enter image description here"></p> <p>For your inner “wedge”, you can use a Kinetic.Group object and use the clipFunc to crop an image into a wedge</p> <p>Here’s the clipFunc for the image-wedge:</p> <pre><code> clipFunc: function(canvas) { var ctx = canvas.getContext(); var w=wedgeWidth; var h=wedgeHeight; var r=wedgeRadius; var angle1=0; // start at vertical var angle2=angle1+radianAngle; ctx.moveTo(cx,cy); ctx.arc(cx,cy,r,angle1,angle2,false); ctx.closePath(); } </code></pre> <p>radianAngle is the variable that controls the size of the wedge.</p> <p>You can set the radianAngle (and the wedge size) like this:</p> <pre><code>// if the user clicks/taps on the stage // redraw the image-wedge to the angle of the click/tap function setRadianAngle(touchX,touchY){ radianAngle=(Math.atan2(touchY-cy,touchX-cx)+Math.PI*2)%(Math.PI*2); layer.draw(); } </code></pre> <p>Here is code and a Fiddle: <a href="http://jsfiddle.net/m1erickson/DxZLJ/" rel="nofollow noreferrer">http://jsfiddle.net/m1erickson/DxZLJ/</a></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Prototype&lt;/title&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"&gt;&lt;/script&gt; &lt;style&gt; #container{ border:solid 1px #ccc; margin-top: 10px; width:400px; height:400px; } &lt;/style&gt; &lt;script&gt; $(function(){ var stage = new Kinetic.Stage({ container: 'container', width: 400, height: 400 }); var layer = new Kinetic.Layer(); stage.add(layer); enableStageClick(); function enableStageClick(){ $(stage.getContent()).on('click tap', function (event) { var pos=stage.getMousePosition(); var mouseX=parseInt(pos.x); var mouseY=parseInt(pos.y); console.log(mouseX+"/"+mouseY); setRadianAngle(mouseX,mouseY); }); } var wedgeWidth=300; var wedgeHeight=300; var wedgeRadius=100; var borderRadius=20; var radianAngle=135*Math.PI/180; var cx=wedgeWidth/2; var cy=wedgeHeight/2; // create the outside "border" circle var border = new Kinetic.Circle({ x: wedgeWidth/2, y: wedgeHeight/2, radius: wedgeRadius+borderRadius, fill:"skyblue", stroke: "blue", strokeWidth: 5 }); layer.add(border); // create the inside "image-wedge" group var wedge=new Kinetic.Group({ x:0, y:0, width:wedgeWidth, height:wedgeHeight, // clip to a wedge with angle==degreeAngle off vertical clipFunc: function(canvas) { var ctx = canvas.getContext(); var w=wedgeWidth; var h=wedgeHeight; var r=wedgeRadius; var angle1=0; // start at vertical var angle2=angle1+radianAngle; ctx.moveTo(cx,cy); ctx.arc(cx,cy,r,angle1,angle2,false); ctx.closePath(); } }); layer.add(wedge); // create the image that will be clipped inside the wedge var image=new Kinetic.Image({ image:image, x:0, y:0, width:wedgeWidth, height:wedgeHeight, }); wedge.add(image); // load the image for the Kinetic Image object var img=new Image(); img.onload=function(){ image.setImage(img); layer.draw(); } img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/KoolAidMan.png"; // if the user clicks/taps on the stage // redraw the image-wedge to the angle of the click/tap function setRadianAngle(touchX,touchY){ radianAngle=(Math.atan2(touchY-cy,touchX-cx)+Math.PI*2)%(Math.PI*2); layer.draw(); } }); // end $(function(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Click/Touch to change angle of wedge-image&lt;/p&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload