Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:55:27+00:00 2026-06-10T00:55:27+00:00

I want to use a sprite in an html5 canvas context. i want to

  • 0

I want to use a sprite in an html5 canvas context.

i want to drag it with my mouse.. so the displayed area changes while dragging the mouse.

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.

I thought i can do this with the drawImage (slice) method, but it doesnt work how i want…

Can you please help me out?

<html>
    <head>
        <script type="text/javascript">

            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 > 50 && event.layerY < 140){
                            y = event.layerY;           
                        }                       
                        clearClock(context);
                        context.drawImage(imageHours, 0, y, 21, 90, 50, 50, 21, 90);                        
                    }
                }, false);              
            }           
        </script>
    </head>
    <body onload="init();">
        <canvas id="uhr" height="500px" width="300px"/>
    </body>
</html>

Sprite:
Sprite:

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:55:28+00:00Added an answer on June 10, 2026 at 12:55 am

    Your question is a little confusing. I think creating a fiddle next time would be a good idea, it helps people help you.

    I think this is what you are trying to do -> jsFiddle

    Two issues I spotted:

    1. Your y value is wrong, it causes the image to not display.
    2. You need to update/redraw your icon on mouseup. You are setting down to false but mousemove wont redraw the icon in that case.

    Edit:

    I’ve updated the fiddle here, this should be what you want and a good starting point for your app. I’d encourage you to read up a bit more on canvas, especially on using images. The Mozzila Dev Network has a great intro to it, check it out for a detailed explanation for why this code works the way it does.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a single image sprite for my web page, where should
I want use many bitmap for my game canvas. I can't Recycle bitmaps, because
I have the following image in a sprite sheet and want to use it
I have a Sprite I want to use as a debug layer. So I
How to through sprite body in upper direction? I want to use box2d for
What I want to do: I want to use sprite sheets to load all
my Main.as extends flash.display.Sprite, I want to use a ComboBox component in the as,
I want Stretch a image. For that i use sprite. I want stretch sprite
I use starling framework, and I want to add flash.display.MovieClip to starling sprite? I
I have sprite brick that I want to use many times in my parallax

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.