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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:54:23+00:00 2026-05-29T05:54:23+00:00

I do know about the case of float/integer values for drawImage ‘s x and

  • 0

I do know about the case of float/integer values for drawImage‘s x and y. But what I need is a smooth animation with an ability to cache my shapes.

  • Article on caching complex paths with backup canvas
  • Article on drawImage with float parameters

For example, I want to draw some complex shape (i.e. SVG-tiger, converted to canvas-commands) to canvas just once and then move it smoothly with ctx.translate and ctx.drawImage. I need the float values then, because instead I get a step-by-step moving:

Here’s the examples at JSFiddle:

  • One: Fast speed, with Math.floor applied to translate parameters (x and y are equal to time in seconds multiplied by 10): Animation is weird (sequential, not smooth).
  • Two: Slow speed, with Math.floor applied to translate parameters (x and y are equal to time in seconds): Animation is weird (sequential, not smooth).
  • Three: Fast speed, no rounding, float values (x and y are equal to time in seconds multiplied by 10). Speed is fast, so animation looks good.
  • Four: Slow speed, no rounding, float values (x and y are equal to time in seconds). Speed is slow, so animation looks pulsating. Why?

The last case is the one that confuses me. Am I wrong in my tryings and there is a possibility to make this caching trick work nice?

In Firefox, there is a property of canvas named mozImageSmoothingEnabled (see), but there is no help from that in other browsers. And it also removes paths smoothing.

Code extract:

var shapeCanvas = null;
var w = 320, h = 240;
var startTime = 0;

function start() {
    startTime = Date.now();
    var docCanvas = document.getElementById('canvas');
    . . .
    shapeCanvas = document.createElement('canvas');
    . . .
    drawShape(shapeCanvas.getContext('2d'));

    drawNext(docCanvas.getContext('2d'));
}

function drawNext(ctx) {
    var msec = (Date.now() - startTime);
    var time = msec / 1000; // seconds passed from start
    ctx.clearRect(0, 0, w, h);

    ctx.save();
    // the lines to change: time | (time * 10) | Math.floor(time * 10)
    ctx.translate((time < 500) ? Math.floor(time * 10) : 500,
                  (time < 500) ? Math.floor(time * 10) : 500);
    ctx.drawImage(shapeCanvas, 0, 0);
    ctx.restore();

    __nextFrame(function() {
       drawNext(ctx);
    });
}

function drawShape(ctx) {
    . . .
}
  • 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-29T05:54:24+00:00Added an answer on May 29, 2026 at 5:54 am

    I wrote the tutorial in your first link.

    Just to clear the air:

    shapeCanvas.style.width = w + 'px';
    shapeCanvas.style.height = h + 'px';
    

    is not really worth doing. No point setting the style if its just a in-memory canvas, and you shouldn’t really ever want to set the width and height style of a canvas anyway, it just confounds things.

    What ellisbben said in the comment is pretty much what’s happening.

    It’s possible to get around it in a few hackish ways I bet. One way might be to make sure its never drawn on an integer pixel. Another might be to use ctx.scale(.99,.99) before drawing anything so it is always anti-aliased. It’s tough to get a consistent solution here because different browswer’s implementations of anti-aliasing are different.

    Here are a few experiments from myself:

    http://jsfiddle.net/KYZYT/29/

    The first two are the shape drawn from a canvas and also drawn from a PNG

    The second two are the same pair but scaled by .99,.99

    The last one is the real thing. It still blurs a bit but looks a lot sharper than using the images.

    None of my experiments lead to an end of your pulsating, at least not on a microscopic level. I think this is just something you’re going to have to live with if you want to animate pixel-perfect images onto half-pixel spaces.

    If you really feel you can’t just draw on perfect pixels then your (second) best bet for consistency is probably to find a way to force anti-aliasing at all times. Making sure you are always translated to a non-integer or scaling it ever so slightly are decent candidates, but there may be others.

    To be honest, you best bet is to not cache these animated paths until you absolutely need the performance from them. Cache the stylized buttons and other static things you make, but if you’ve got animated paths that need to move slowly and precisely and look very good, you might as well stick with the true thing over my caching optimization unless you really need it for those too.

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

Sidebar

Related Questions

I know about SortedSet , but in my case I need something that implements
I don't know much about database optimization, but I'm trying to understand this case.
I know about camel case rules, but I'm confused with this m rule. What
I know about Application.Exit() but what if I'm not in a windows forms application
I know about the HIG (which is quite handy!), but what programming practices do
I know about code-behind files, but what is the best real-world way of Designers
Prologue: I know about gems. I use Drupal at work. In case you don't
I know about using _ instead of & , but I'm looking at all
I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
I know about using a -vsdoc.js file for IntelliSense , and the one for

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.