Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I’m guessing the source image for your smaller circle is the same as the big circle--just unrotated.</p> <p>If that’s the case, you need to <strong>find your un-rotated mouseclick position.</strong></p> <p>Then you can clip your smaller circle around the unrotated mouseclick and rotate it into place.</p> <p>Given:</p> <ul> <li>Your original center of rotation is cx/cy,</li> <li>Your angle of rotation is radAngle (in radians)</li> <li>Your rotated mousclick point is rx/ry.</li> </ul> <p>You can calculate your un-rotated mouseclick position ( preRX / preRY ) like this:</p> <pre><code>// the bigger circle's rotation point var cx=150; var cy=150; // the rotation angle in radians var radAngle=Math.PI/6; // 30 degrees, for example // the rotated mouseclick point var rx=225; var ry=245; // the radius of the smaller circle var smallRadius=50; // calculate the unrotated mouseclick point var dx= rx-cx; var dy= ry-cy; var preRX= cx+ dx*Math.cos(-radAngle) - dy*Math.sin(-radAngle); var preRY= cy+ dy*Math.cos(-radAngle) + dx*Math.sin(-radAngle); </code></pre> <p>Then you make a clipping path using your smaller circle</p> <pre><code>// create a clipping path for the small circle ctx.arc(preRX,preRY,smallRadius,0,Math.PI*2,false); ctx.closePath(); ctx.clip(); </code></pre> <p>And finally translate + rotate and draw the clipped image into position</p> <pre><code>// rotate the small circle into position ctx.translate(cx,cy); ctx.rotate(radAngle); ctx.globalAlpha=1.0; ctx.drawImage(img,-img.width/2,-img.height/2); </code></pre> <p>Here is code and a Fiddle: <a href="http://jsfiddle.net/m1erickson/YAt5r/" rel="nofollow">http://jsfiddle.net/m1erickson/YAt5r/</a></p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /&gt; &lt;!-- reset css --&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery.min.js"&gt;&lt;/script&gt; &lt;style&gt; body{ background-color: ivory; } canvas{border:1px solid red;} &lt;/style&gt; &lt;script&gt; $(function(){ var canvas = document.getElementById('canvas'); var ctx=canvas.getContext("2d"); var img=new Image(); img.onload=function(){ draw(); } img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house-icon.png"; function draw(){ // the bigger circle's rotation point var cx=150; var cy=150; // the rotation angle in radians var radAngle=Math.PI/6; // 30 degrees // the rotated mouseclick point var rx=225; var ry=245; // the radius of the smaller circle var smallRadius=50; // calculate the unrotated mouseclick point var dx= rx-cx; var dy= ry-cy; var preRX= cx+ dx*Math.cos(-radAngle) - dy*Math.sin(-radAngle); var preRY= cy+ dy*Math.cos(-radAngle) + dx*Math.sin(-radAngle); // test // rotate the original image and draw it ctx.save(); ctx.translate(cx,cy); ctx.rotate(radAngle); ctx.globalAlpha=.25; ctx.drawImage(img,-img.width/2,-img.height/2); ctx.restore(); // clip the smaller image, rotate it and draw it ctx.save(); ctx.beginPath(); // create a clipping path for the small circle ctx.arc(preRX,preRY,smallRadius,0,Math.PI*2,false); ctx.closePath(); ctx.clip(); // rotate the small circle into position ctx.translate(cx,cy); ctx.rotate(radAngle); ctx.globalAlpha=1.0; ctx.drawImage(img,-img.width/2,-img.height/2); // stroke the circle ctx.arc(preRX,preRY,smallRadius,0,Math.PI*2,false); ctx.stroke(); ctx.restore(); } }); // end $(function(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Original image is 25% opacity&lt;/p&gt; &lt;p&gt;Clipped overlay image is 100% opacity&lt;/p&gt; &lt;canvas id="canvas" width=350 height=350&gt;&lt;/canvas&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.
    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