What is web worker for? OK, read it docs to get full details idea. Or just a quick intro to web worker.

Web worker, open another thread in the background sprated from main thread. You can just think Web worker is a async function...

OK, so what does web worker good for? Improve the profermence! Imaging there is some code which need to handle image transform, if you put the whole thing in the main thread, it will really jank! It freaze your browser for second, it has a poor profermence.

Instead, you can put image transform code into a web worker, let it help you to handle the heavy code in the background.

To use web worker, only need to do two things:

1. Register a web worker.

You need to create a 'worker.js', name it whatever you want.

var imageWorker = new Worker('scripts/worker.js');

The code should be run in early stage, or let sya idle stage.

2. Communiate between web worker file and your component file by using: 'postMessage' and <worker>.onmessage:

// your component
function manipulateImage(type) {
var a, b, g, i, imageData, j, length, pixel, r, ref;
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
toggleButtonsAbledness();
imageWorker.postMessage({'imageData': imageData, 'type': type}); imageWorker.onmessage = function(e) {
toggleButtonsAbledness();
var image = e.data;
if (image) return ctx.putImageData(e.data, 0, 0);
console.log("No manipulated image returned.")
} imageWorker.onerror = function(error) {
function WorkerException(message) {
this.name = "WorkerException";
this.message = message;
};
throw new WorkerException('Worker error.');
};
};
// require some js files on the top
importScripts('imageManips-improved.js'); // listen for the message
this.onmessage = function(e) {
var imageData = e.data.imageData;
var type = e.data.type; try {
length = imageData.data.length / 4;
var manipulatePixel = getManipFunc(type);
for (i = j = 0, ref = length; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
r = imageData.data[i * 4 + 0];
g = imageData.data[i * 4 + 1];
b = imageData.data[i * 4 + 2];
a = imageData.data[i * 4 + 3];
pixel = manipulatePixel(r, g, b, a);
imageData.data[i * 4 + 0] = pixel[0];
imageData.data[i * 4 + 1] = pixel[1];
imageData.data[i * 4 + 2] = pixel[2];
imageData.data[i * 4 + 3] = pixel[3];
}
// send message back to the component
postMessage(imageData);
} catch (e) {
function ManipulationException(message) {
this.name = "ManipulationException";
this.message = message;
};
throw new ManipulationException('Image manipulation error');
postMessage(undefined);
}
}

[Web Worker] Introduce to Web Worker的更多相关文章

  1. 在win 7 vs2013下 web 调试 出现“ iis Express Worker Process 已停止工作”错误

    在win 7 vs2013下 web 调试 出现“ iis Express Worker Process 已停止工作”错误: 如下图:   最终解决方案如下: 用管理员身份运行CMD,输入netsh  ...

  2. electron项目中使用js web worker时,new worker(path)路径问题

    如题,在new worker时需要传入js文件路径,可是在electron环境中使用出现问.同目录下,recorder.jsworker.js recorder.js中调用 var path = '. ...

  3. 天人合一物我相融,站点升级渐进式Web应用PWA(Progressive Web Apps)实践

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_216 PWA(Progressive web apps,渐进式 Web 应用)使用现代的 Web API 以及传统的渐进式增强策略 ...

  4. 手机H5 web调试利器——WEINRE (WEb INspector REmote)

    手机H5 web调试利器--WEINRE (WEb INspector REmote) 调试移动端页面,优先选择使用chrome浏览器调试,如果是hybrid形式的页面,可以使用chrome提供的ch ...

  5. Atitit.web三大编程模型 Web Page Web Forms 和 MVC

    Atitit.web三大编程模型 Web Page    Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2.  ...

  6. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  7. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...

  8. Rest风格WEB服务(Rest Style Web Service)的真相

    http://blog.csdn.net/jia20003/article/details/8365585 Rest风格WEB服务(Rest Style Web Service)的真相 分类: J2E ...

  9. [Web API] 如何让 Web API 统一回传格式以及例外处理[转]

    [Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...

随机推荐

  1. CodeForces 660A

    Description You are given an array of n elements, you must make it a co-prime array in as few moves ...

  2. Polyfill 与 Shim

    Polyfill 与 Shim polyfill 的概念是 Remy Sharp 在2010年提出的. polyfill,或 polyfiller ,表示为开发人员提供旧浏览器没有原生支持的较新功能的 ...

  3. [XJOI]noip43 T2多人背包

    多人背包 DD 和好朋友们要去爬山啦!他们一共有 K 个人,每个人都会背一个包.这些包的容量是相同的,都是 V.可以装进背包里的一共有 N 种物品,每种物品都有给定的体积和价值.在 DD 看来,合理的 ...

  4. HBase编程 API入门系列之工具Bytes类(7)

    这是从程度开发层面来说,为了方便和提高开发人员. 这个工具Bytes类,有很多很多方法,帮助我们HBase编程开发人员,提高开发. 这里,我只赘述,很常用的! package zhouls.bigda ...

  5. C# net winform wpf 发送post数据和xml到网页

    由于项目需要发送数据到网页 这里用aspx做测试 采用post以及get发送数据,页面进行数据  首先这个东西很简单很简单,基本上学过的都会,但是原谅一直搞cs几乎不搞bs的猿类吧.三四年没接触bs. ...

  6. ROW_NUMBER() OVER()函数用法;(分组,排序),partition by (转)

    1.row_number() over()排序功能: (1) row_number() over()分组排序功能: 在使用 row_number() over()函数时候,over()里头的分组以及排 ...

  7. .net 对称加密

    后台   public class CryptoHelper     {         // 对称加密算法提供器         private ICryptoTransform encryptor ...

  8. 在windows系统下虚拟机和ubuntu系统的安装和卸载

    一.安装 之前有装过双系统,但是吧,一直用ubuntu系统,很久没进windows之后的某一天,自己再进windows,发现windows系统崩了,我也不知道为什么,找了很多方法没有解决,最后只好重装 ...

  9. mysqldump+mydumper+xtrabackup备份原理流程

    mysqldump备份原理 备份的基本流程如下: 1.调用FTWRL(flush tables with read lock),全局禁止读写 2.开启快照读,获取此时的快照(仅对innodb表起作用) ...

  10. UML+模式设计概述

    转自于:http://blog.csdn.net/rexuefengye/article/details/13020225 工程学:工程庞大到一定程度必须是用工程学方法,好比直接用水泥沙子建设实用的摩 ...