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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:19:04+00:00 2026-06-04T15:19:04+00:00

I am rendering a sprite to a canvas element, and I am trying to

  • 0

I am rendering a sprite to a canvas element, and I am trying to avoid sub-pixel positioning in order to avoid blurry images (based on information here: http://seb.ly/2011/02/html5-canvas-sprite-optimisation/).

My problem happens when I try to reassign the x position like such:

this.pos.x = ~~ this.pos.x;

this keeps my sprite from ever moving on the screen, and logging its position to the consol shows I am stuck at my starting position.

Now, if I do not perform the bitwise operator on my position, and simply log the output:

console.log(~~ this.pos.x);

I can see the bitwise operator performing as expecting, showing:

100
101
102
...

Note: My animation is framerate independent, and thus I am multiplying it by a delta and getting floats like:

100.64
101.36
102.04
102.68
103.32000000000001
104.00000000000001
104.68000000000002
...

I update the position here, and only here:

this.pos.x += BE.delta.getSeconds() * this.accelleration;

and thus

this.pos.x += ~~ (BE.delta.getSeconds() * this.accelleration);

is not working

  • 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-04T15:19:06+00:00Added an answer on June 4, 2026 at 3:19 pm

    The bitwise NOT operator (~) just truncates the fractional portion off the number (e.g., ~~100.1 is 100; ~~-100.1 is -100). So naturally if you’re adding values of less than 1 to this.pos.x, if you do this.pos.x = ~~this.pos.x, you’re never going to see it increase, because you’re constantly wiping out those fractional values.

    You haven’t provided a lot of context, but it may be that you want to allow this.pos.x to remain a floating point number, but use a floored version of it for rendering, e.g.:

    var renderX = ~~this.pos.x;
    // Use `renderX` rather than `this.pos.x` to render to the canvas;
    // `renderX` will remain 100 while `this.pos.x` will gradually climb
    // from 100 to 100.1, 100.5, 100.999999, etc. Once `this.pos.x`
    // reaches 101 (or 101.1, etc.), `renderX` will become 101.
    

    Separately, it may be more appropriate to round rather than flooring. If this.pox.x is 100.999999999, surely it makes more sense to render it at 101 than 100?

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

Sidebar

Related Questions

While rendering the view page, based on some condition in the controller action I
MRT allows for rendering to multiple texture targets in the pixel shader, but I'm
In the process of trying to port my 2D shadow rendering technique from Directx
I'm trying to create a small 2D game in Javascript/Canvas which consists of several
I am using OpenGL 1.3 to do 2D sprite rendering and supporting both POTS
When I try to draw an image to a Canvas element I get this
I'm trying to show 1000 quite small images on a page (rather a lot
What underlying rendering system does the Qt GUI toolkit use on Windows? (I'm assuming
Getting a rendering error for this form: 'NoneType' object has no attribute 'widget' http://dpaste.com/88585/
I'm rendering a form. I would like to put a mark next to all

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.