Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Html5 Canvas can do your indicator using it’s clipping capability</strong></p> <p><img src="https://i.stack.imgur.com/Sekq6.png" alt="enter image description here"><img src="https://i.stack.imgur.com/y8fFV.png" alt="enter image description here"></p> <p>Here’s the way it works:</p> <ul> <li>Put the canvas in a div.</li> <li>Tell the canvas where to position the indicator (the "Y" coordinate).</li> <li>Tell the canvas which image you want to clip inside your irregular canvas div.</li> </ul> <p>This is the function you call to change the canvas image and indicator:</p> <pre><code> drawCanvasDiv(indicatorYPosition,yourImage); </code></pre> <p>This is the code that draws the irregular box with the image clipped inside:</p> <p>Once you style your irregular canvas-div exactly as you like it…this code will not change.</p> <pre><code> function drawCanvasDiv(indicatorY,img){ ctx.save(); ctx.clearRect(0,0,w,h); // this call will define the path inside which the image will be clipped definePath(indicatorY); // clip any image inside the irregularly shaped path ctx.clip(); // draw your desired image ctx.drawImage(img,0,0,img.width,img.height); // finally draw the gray border of the irregular shape definePath(indicatorY); ctx.lineWidth=2; ctx.strokeStyle="lightgray"; ctx.stroke(); ctx.restore(); } </code></pre> <p>Here’s code and a Fiddle: <a href="http://jsfiddle.net/m1erickson/KYMFn/" rel="nofollow noreferrer">http://jsfiddle.net/m1erickson/KYMFn/</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; padding:20px; } button{ margin:20px; } &lt;/style&gt; &lt;script&gt; $(function(){ var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var canvasOffset=$("#canvas").offset(); var offsetX=canvasOffset.left; var offsetY=canvasOffset.top; var w=canvas.width; var h=canvas.height; var leftOffset=15; var indicatorHeight=20; var currentY=125; var img=new Image(); img.onload=function(){ drawCanvasDiv(currentY,img); } img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/skyGrass.png"; function drawCanvasDiv(indicatorY,img){ ctx.save(); ctx.clearRect(0,0,w,h); definePath(indicatorY); ctx.clip(); ctx.drawImage(img,0,0,img.width,img.height); definePath(indicatorY); ctx.lineWidth=2; ctx.strokeStyle="lightgray"; ctx.stroke(); ctx.restore(); } function definePath(indicatorY){ ctx.beginPath(); ctx.moveTo(leftOffset,0); ctx.lineTo(w,0); ctx.lineTo(w,h); ctx.lineTo(leftOffset,h); ctx.lineTo(leftOffset,indicatorY+indicatorHeight/2); ctx.lineTo(0,indicatorY); ctx.lineTo(leftOffset,indicatorY-indicatorHeight/2); ctx.closePath(); } $("#up").click(function(){ if(currentY-20-indicatorHeight/2&gt;0){ currentY-=20; drawCanvasDiv(currentY,img); } }); $("#down").click(function(){ if(currentY+20+indicatorHeight/2&lt;h){ currentY+=20; drawCanvasDiv(currentY,img); } }); }); // end $(function(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;canvas id="canvas" width=300 height=248&gt;&lt;/canvas&gt;&lt;br&gt; &lt;button id="up"&gt;Indicator Up&lt;/button&gt; &lt;button id="down"&gt;Indicator Down&lt;/button&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