English 中文(简体)
Jerky canvas animation on Mapbox map
原标题:

I m trying to animate canvas marker along the route on Mapbox map (mapbox-gl). But canvas animation looks really jerky, especially when animation is slow.

Here is the example https://codepen.io/vps-dev/pen/MWzqpRx

Canvas image creation:

const canvasMarker = {
      width: size,
      height: size,
      data: new Uint8Array(size * size * 4),
      context: null,

      // When the layer is added to the map,
      // get the rendering context for the map canvas.
      onAdd: function () {
        const canvas = document.createElement( canvas )
        canvas.width = this.width
        canvas.height = this.height
        this.context = canvas.getContext( 2d )
      },

      // Call once before every frame where the icon will be used.
      render: function () {
        const context = this.context
        if (!context) return false

        context.clearRect(0, 0, context.canvas.width, context.canvas.height)
        context.beginPath()

        context.font =  30px Arial 
        context.fillText( Hello World , 0, 20)
        context.rect(0, 40, this.width, 10)
        context.rect(0, 55, this.width, 10)
        context.fillStyle =  #000 
        context.fill()

        this.data = context.getImageData(0, 0, this.width, this.height).data
        
        map.triggerRepaint()

        // Return `true` to let the map know that the image was updated.
        return true
      },
    };

Adding image on map:

map.addImage( canvas-marker , canvasMarker, { pixelRatio: 2 })
      
map.addLayer({
  id :  point ,
  source :  point ,
  type :  symbol ,
  layout : {
    icon-image :  canvas-marker ,
    icon-size : 1.5,
     icon-allow-overlap : true,
     icon-ignore-placement : true
  }
});

Then animating point with requestAnimationFrame

Can you please support, what am I doing wrong? Thank you for your help!

问题回答

I think your issue is that you are expecting requestAnimationFrame() to be called at precisely even intervals, and it won t.

You need to keep track of how long it was since the last animation update, and then move the marker the proportional distance. So if it was 5ms, you move 5km, and if it was 30ms, you move 30km, for instance.

You can see this in the example here: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

So your animate() function should make use of the argument passed to it: animate(timeStamp) and then use that instead of incrementing a counter.





相关问题
ZOrder with Canvas within the List

我有一份清单,其中载有一些控制措施,包括一名扩大成员。 在扩大小组内还有另一个名单,我想把这份清单放在外。 这里简单地重提:

Detecting drawn shapes in Canvas+Javascript?

I had an idea for a WebOS, but it requires detecting drawn shapes. Ie: I want a user to be able to draw an image, then draw a big box around the whole image. Then the user can drag that box by the ...

Canvas Element and IE

Well not just IE, any browser that doesn t currently support it I want to start using the processing.js framework. It s used for making images with the canvas element. However I m reluctant to use it ...

Bezier timed animation path

I m trying to define a path of points. Each point has an x, y, and time. I then want to query this path and get the current position at that point in time. Let me share some pseudo code. point {x, ...

Rendering web page as thumbnail

I asked this question earlier, since I am a little new to Firefox extension development, the canvas thing for Firefox went a little over my head. Apologies for reposting this again. with the first ...

热门标签