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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:26:08+00:00 2026-05-22T03:26:08+00:00

I’m writing a program that will draw the sine curve with canvas. HTML: <canvas

  • 0

I’m writing a program that will draw the sine curve with canvas.
HTML:

<canvas id="mycanvas" width="1000" height="100">
    Your browser is not supported.
</canvas>

JavaScript:

var canvas = document.getElementById("mycanvas");
if (canvas.getContext) {
    var ctx = canvas.getContext("2d");
    ctx.lineWidth = 3;
    var x = 0,
        y = 0;
    var timeout = setInterval(function() {
        ctx.beginPath();
        ctx.moveTo(x, y);
        x += 1;
        y = 50 * Math.sin(0.1 * x) + 50;
        ctx.lineTo(x, y);
        ctx.stroke();
        if (x > 1000) {
            clearInterval(timeout);
        }
    }, 10);
}

This works really nice: http://jsfiddle.net/HhGnb/

However, now I can only offer say 100px for the canvas width, so only the leftest 100px of the curve could be seen. http://jsfiddle.net/veEyM/1/
I want to archive this effect: when the right point of the curve is bigger than the width of canvas, the whole curve could move left, so I can see the rightest point of the curve, it’s a bit like the curve is flowing to left. Can I do that?

  • 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-22T03:26:08+00:00Added an answer on May 22, 2026 at 3:26 am

    One of the basic ideas of the <canvas> element is that the computer ‘forgets’ the drawing commands and only saves the pixels, like a bitmap. So to move everything to the left, you need to clear the canvas and draw everything again.

    There is also one thing I’d like to advise you – you always start with x = 0 and y = 0, but obviously at x = 0 then y is not necessarily equal to 0 as well. EDIT: implemented this.

    Anyway, I ended up with this code: http://jsfiddle.net/veEyM/5/

    var canvas = document.getElementById("mycanvas");
    var points = {}; // Keep track of the points in an object with key = x, value = y
    var counter = 0; // Keep track when the moving code should start
    
    function f(x) {
        return 50 * Math.sin(0.1 * x) + 50;
    }
    
    if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        ctx.lineWidth = 3;
        var x = 0,
            y = f(0);
        var timeout = setInterval(function() {
            if(counter < 100) { // If it doesn't need to move, draw like you already do
                ctx.beginPath();
                ctx.moveTo(x, y);
                points[x] = y;
                x += 1;
                y = f(x);
                ctx.lineTo(x, y);
                ctx.stroke();
                if (x > 1000) {
                    clearInterval(timeout);
                }
            } else { // The moving part...
                ctx.clearRect(0, 0, 100, 100); // Clear the canvas
                ctx.beginPath();
                points[x] = y;
                x += 1;
                y = f(x);
                for(var i = 0; i < 100; i++) {
                    // Draw all lines through points, starting at x = i + ( counter - 100 )
                    // to x = counter. Note that the x in the canvas is just i here, ranging
                    // from 0 to 100
                    ctx.lineTo(i, points[i + counter - 100]);
                }
                ctx.stroke();
            }
            counter++;
        }, 10);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

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.