javascript - Is there a way to use a web worker to resize an image client side? - Stack Overflow

The way I'm resizing images now is by sticking it into a canvas element and then scaling the conte

The way I'm resizing images now is by sticking it into a canvas element and then scaling the context of the canvas. The problem is, when I'm resizing many images the UI basically freezes. Is there anyways I can move this resizing step to a web worker? Problem I'm having is that you can't use document.createElement('canvas') or Image(), two functions crucial to this implementation.

The way I'm resizing images now is by sticking it into a canvas element and then scaling the context of the canvas. The problem is, when I'm resizing many images the UI basically freezes. Is there anyways I can move this resizing step to a web worker? Problem I'm having is that you can't use document.createElement('canvas') or Image(), two functions crucial to this implementation.

Share Improve this question asked Jun 3, 2016 at 6:50 WilfredWilfred 8092 gold badges18 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

It is possible. However, because canvas isn't available in a worker, you would have to use your own/3rd party code to manipulate the image data in the worker.

For example, you could use https://github./nodeca/pica, which quite handily does its processing in a web worker if web workers are supported.

A rough example of using this to resize image from an img element to a canvas element...

<button onclick="resize()">Resize</button>
<img id="original" src="my-image.jpg">
<canvas id="resized">

With Javascript

function resize() {
  // Find the original image
  var originalImg = document.getElementById("original");

  // Create an empty canvas element of the same dimensions as the original
  var originalCanvas = document.createElement("canvas");
  originalCanvas.width = originalImg.width;
  originalCanvas.height = originalImg.height;

  // Copy the image contents to the canvas
  var ctx = originalCanvas.getContext("2d");
  ctx.drawImage(originalImg, 0, 0);

  // Set the target dimensions
  var resizedCanvas = document.getElementById("resized");
  resizedCanvas.width = originalCanvas.width / 2;
  resizedCanvas.height = originalCanvas.height / 2;

  // Resize (using web workers if supported)
  pica.resizeCanvas(originalCanvas, resizedCanvas, {}, function(err) {
    // Do something on finish/error
  }); 
}

Which can be seen at https://plnkr.co/edit/yPRjxqQkHryqeZKw4YIH?p=preview

Unfortunately, you cannot use integrated browser functions for that. Instead, you need to obtain pixel data:

var data = ctx.getImageData(0,0,canvas.width, canvas.height);

You need to send those to worker. You can use transfer mode for the array:

worker.postMessage( {
                     name: "image_data",
                     data: data.data,
                     width: data.width,
                     height: data.height
                    },
                     [data.data] // this tells browser to transfer data to web worker
);

I modified function from some other answer so that it can scale image using the image data array. It's quite limited, as the scale is only allowed to be integer - that means you can't scale down: https://jsfiddle/n3drn8v9/5/

I remend googling some libraries for this, rather than reinventing the wheel.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629535a4637020.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信