[Web Worker] Introduce to Web Worker
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的更多相关文章
- 在win 7 vs2013下 web 调试 出现“ iis Express Worker Process 已停止工作”错误
在win 7 vs2013下 web 调试 出现“ iis Express Worker Process 已停止工作”错误: 如下图: 最终解决方案如下: 用管理员身份运行CMD,输入netsh ...
- electron项目中使用js web worker时,new worker(path)路径问题
如题,在new worker时需要传入js文件路径,可是在electron环境中使用出现问.同目录下,recorder.jsworker.js recorder.js中调用 var path = '. ...
- 天人合一物我相融,站点升级渐进式Web应用PWA(Progressive Web Apps)实践
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_216 PWA(Progressive web apps,渐进式 Web 应用)使用现代的 Web API 以及传统的渐进式增强策略 ...
- 手机H5 web调试利器——WEINRE (WEb INspector REmote)
手机H5 web调试利器--WEINRE (WEb INspector REmote) 调试移动端页面,优先选择使用chrome浏览器调试,如果是hybrid形式的页面,可以使用chrome提供的ch ...
- Atitit.web三大编程模型 Web Page Web Forms 和 MVC
Atitit.web三大编程模型 Web Page Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2. ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- 转 web项目中的web.xml元素解析
转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45) 阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...
- Rest风格WEB服务(Rest Style Web Service)的真相
http://blog.csdn.net/jia20003/article/details/8365585 Rest风格WEB服务(Rest Style Web Service)的真相 分类: J2E ...
- [Web API] 如何让 Web API 统一回传格式以及例外处理[转]
[Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...
随机推荐
- POJ3614 Sunscreen 贪心入门
题目大意 给出一些区间和一些点,一个点如果在一个区间内,那么此两者可以匹配.问匹配数最大是多少. 题解 这样的题我们一般都是站在区间上去找与其配对的点.我们可以得到如下性质: 对于一段区间\([l_1 ...
- luogu3911 最小公倍数之和
题目大意 给出一些数\(A_1,A_2,\cdots A_n\),求 \[\sum_{i=1}^{n}\sum_{j=1}^{n}\mathrm{lcm}(A_i,A_j)\] \(A_i,A_n\l ...
- php模版静态化原理
看了一些开源系统的,简单的总结一下php的模板及静态原理. 先贴代码,再做解释. index.php <?php //如果已存在静态页面,直接读取并显示 if(file_exists('inde ...
- Hdu-5983 2016ACM/ICPC亚洲区青岛站 B.Pocket Cube 模拟
题面 题意:给你一个2*2的魔方,给你每个面每个小块的颜色,一共24个,然后问你能否在一步之内还原. 题解:手动在纸上画,推出每种变化对应的置换,显然,一共有6种,而且可以当成3种,(具体哪3种,就是 ...
- C# 同步更新系统时间
前言 在定位用户问题时,发现有些电脑,会出现系统时间不是最新的问题. 可能原因: 取消了勾选服务器时间同步 当前安装的系统,是一个未知来源系统,导致系统时间更新失败 而系统时间不正确,会导致IE选项- ...
- python 飞机大战 实例
飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...
- A - A Compatible Pair
Problem description Nian is a monster which lives deep in the oceans. Once a year, it shows up on th ...
- 实现model中的文件上传FTP(一)
由于在django的model中配置了filefield或者imagefield配置了upload_to参数只能将用户上传的文件上传到项目本地,就算重定向到项目外也只是直接读取文件系统,这样对未来的项 ...
- tomcat 使用常见问题
tomcat 下目录的介绍: 参考地址: http://blog.csdn.net/u013132035/article/details/54949593 参考书籍:tomcat权威指南 (1) 非w ...
- 超酷消息警告框插件(SweetAlert)
今天给大家推荐一款不错的超酷消息警告框–SweetAlert:SweetAlert是一款不需要jQuery支持的原生js提示框,风格类似bootstrap.它的提示框不仅美丽动人,并且允许自定义,支持 ...