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 7174579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:05:11+00:00 2026-05-28T16:05:11+00:00

I am a newb when it comes to HTML5 and the infamous Canvas element.

  • 0

I am a newb when it comes to HTML5 and the infamous Canvas element. Currently, I have a blue ball drawn on my webpage and on click of the canvas element, the ball slides up to a position (Y) that I pass to a drawCircle function. I am wanting to slideUp the ball to the Y location, vs the ball jumping to the Y location.

Here is my code thus far:

    var context, canvas;
var x = 100;
var y = 200;
var dx = 5;
var dy = .02;

function drawCircle(move) {
    if(move) {
        x = move.x
        y = move.y
    }

    canvas = document.getElementById('myCanvas');
    context = canvas.getContext('2d')
    context.clearRect(0,0,canvas.width, canvas.height);
    context.beginPath()
    context.fillStyle = "#0000ff";
    context.arc(x, y, 20, 0, Math.PI*2, true);
    context.closePath();
    context.fill();
    // if(x < 0 || x > canvas.width) dx=-dx;
    // if(y < 0 || y > canvas.height) dy =- dy;

    if(move) {
            y+=dy
    }

    // x+=dx
    // y+=dy
}

window.onload = function(e){
    // setInterval(drawCircle, 10)
    drawCircle()
    canvas.onclick = function(){
        var move = {
            x: 100,
            y: 100
        }
        drawCircle(move)
    }
}

JSFiddle: http://jsfiddle.net/Uam8z/1/

  • 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-05-28T16:05:11+00:00Added an answer on May 28, 2026 at 4:05 pm

    You don’t need to keep defining the canvas and setting the context. This handles sliding up:

    var context, canvas, target;
    var x = 100;
    var y = 200;
    var dx = 5;
    var dy = 2; //.2 is pretty slow
    
    function drawCircle() {
    
        // sliding up
        if (y > target) {
            y -= dy;
        }
            context.clearRect(0, 0, canvas.width, canvas.height);
            context.beginPath()
            context.fillStyle = "#0000ff";
            context.arc(x, y, 20, 0, Math.PI * 2, true);
            context.fill();
            context.closePath();
    
    }
    
    window.onload = function () {
    
        // get canvas, and context once
        canvas = document.getElementById('myCanvas');
        context = canvas.getContext('2d');
    
        // set target y position
        canvas.onclick = function (e) {
            target = e.clientY;
        }
        // 30fps
        setInterval(drawCircle, (1000 / 30));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newb when it comes to javascript. Perhaps somebody could help me with
I'm a total newb to LINQ. Here is the code I have so far:
I am a newb when it comes to JSON and am wanting to try
First of all, I must state that I'm a complete newb when it comes
I have written a WCF service (I am a newb) that I want to
I'm a little bit of a newb when it comes to PHP, but know
Alright, I am an extreme newb when it comes to coding and I am
Ok, I know this is probably a pretty newb question, but when it comes
I'm a relative newb when it comes to regexes, but i'm starting to get
I'm a complete newb when it comes to regex and I need a way

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.