Note that there are some explanatory texts on larger screens.

plurals
  1. POhtml5 canvas using sprite
    primarykey
    data
    text
    <p>I want to use a sprite in an html5 canvas context.</p> <p>i want to drag it with my mouse.. so the displayed area changes while dragging the mouse.</p> <p>It should look like a window with width: 21 and height: 90, where just a part of the sprite is visible. when i drag the sprite downwards or upwards i want the visible part to change.</p> <p>I thought i can do this with the drawImage (slice) method, but it doesnt work how i want...</p> <p>Can you please help me out?</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; var y = 30; var canvas; var context; var imageHours; function clearClock(context) { context.clearRect(0, 0, 300, 500); } function init(){ canvas = document.getElementById("uhr"); context = canvas.getContext("2d"); imageHours = new Image(); imageHours.src = "07.png"; context.drawImage(imageHours, 0, y, 21, 90, 50, 50, 21, 90); var down = false; canvas.addEventListener('mousedown', function(event) { down = true; }, false); canvas.addEventListener('mouseup', function() { down = false; }, false); canvas.addEventListener('mousemove', function(event){ if(down){ if(event.layerY &gt; 50 &amp;&amp; event.layerY &lt; 140){ y = event.layerY; } clearClock(context); context.drawImage(imageHours, 0, y, 21, 90, 50, 50, 21, 90); } }, false); } &lt;/script&gt; &lt;/head&gt; &lt;body onload="init();"&gt; &lt;canvas id="uhr" height="500px" width="300px"/&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Sprite: <img src="https://i.stack.imgur.com/hD1Ij.png" alt="Sprite:"></p> <p>EDIT: i added a comment to my mainPost. the reason why i am trying to acomplish this like that is.. that i dont want to use a library for this.. because i want to learn such stuff... using libraries doesnt help me with learning...</p>
    singulars
    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.
 

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