Horrible Canvas GetImageData() / PutImageData() performance on mobile

前端 未结 2 1415
梦如初夏
梦如初夏 2021-02-15 15:24

I\'m doing a little HTML5 game and, while loading my sprites at the beginning of the map, I do some processing with GetImageData() / looping over all the image / PutImageData().

2条回答
  •  庸人自扰
    2021-02-15 15:56

    Have you seen this performance tip?: http://ajaxian.com/archives/canvas-image-data-optimization-tip

    They talk about reducing calls to the DOM to increase performance.

    So, based on this tip you might try:

    var pixel = ctx.getImageData(0, 0, tmpCanvas.width, tmpCanvas.height);
        pixelData=pixel.data; // detach the pixel array from DOM
        var length = pixelData.length;
        for (var i = 0; i < length; i+= 4) {
            pixelData[i] = pixelData[i] * ratio;
            pixelData[i + 1] = pixelData[i + 1] * ratio;
            pixelData[i + 2] = pixelData[i + 2] * ratio;
        }
    

    Your .data calls may be slowing you down.

提交回复
热议问题