web worker 的 self
- A
self
object, which is the global object representing the worker in this scope. - 对self对象的译法,未知妥否。
1 // Call the invertImage method when this worker receives a message from the calling script. 2 // The ‘self’ object contains the only methods a web worker can access apart from those it 3 // defines and creates itself 4 //当此worker收到自来于发出调用的脚本的消息时,调用invertImage方法。“self”对象所包含的web worker所能访问的方法就只有那些由web worker自己本身进行定义和创建的方法。 5 self.addEventListener("message", invertImage, false); 6 7 // Define a function to take an image and invert it, pixel by pixel, using its raw data 8 //定义一个函数,用作接收图像的原始数据,按像素来逐粒逐粒地实施反相操作。 9 function invertImage(e) { 10 11 // The ‘data’ property of the ‘message’ event contains the pixel data passed from 12 // the calling script 13 //“message”事件的“data”属性包含着从发出调用的脚本所包含的像素数据。 14 var message = e.data, 15 16 // The ‘data’ property of the message passed contains the raw image pixel data 17 //所发送过来的消息中的“data”属性包含着原始图像的像素数据。 18 imagePixels = message.data, 19 x = 0, 20 len = imagePixels.length; 21 22 // Loop through each pixel, inverting its value within the original pixel data array. 23 // Pixel data is arranged in groups of 4 values, representing the red, green, blue, and 24 // opacity values of each visible screen pixel. We therefore loop through in jumps of 4 25 // on each iteration 26 //循环遍历每一粒像素,使原始像素数据的数组中所保存的值反相。像素数据按4个值进行分组,分别表示屏幕上所见像素的红、绿、蓝和透明度数值。因此,循环的每轮迭代的加数为4。 27 for (; x < len; x += 4) { 28 29 // To invert a pixel’s value, subtract it from the maximum possible value, which is 255 30 //要对像素值进行反相,可以用最大的可能值(即255)进行相减。 31 imagePixels[x] = 255 - imagePixels[x]; 32 imagePixels[x + 1] = 255 - imagePixels[x + 1]; 33 imagePixels[x + 2] = 255 - imagePixels[x + 2]; 34 } 35 36 // Finally, post a message containing the updated pixel data back to the calling script 37 //最后,把包含着更新后的像素数据的消息发送回至发出调用的脚本。 38 self.postMessage(message); 39 }
---------------------------------------------------------------end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!