<bdo id='bK2V8'></bdo><ul id='bK2V8'></ul>
  1. <tfoot id='bK2V8'></tfoot>

    <i id='bK2V8'><tr id='bK2V8'><dt id='bK2V8'><q id='bK2V8'><span id='bK2V8'><b id='bK2V8'><form id='bK2V8'><ins id='bK2V8'></ins><ul id='bK2V8'></ul><sub id='bK2V8'></sub></form><legend id='bK2V8'></legend><bdo id='bK2V8'><pre id='bK2V8'><center id='bK2V8'></center></pre></bdo></b><th id='bK2V8'></th></span></q></dt></tr></i><div id='bK2V8'><tfoot id='bK2V8'></tfoot><dl id='bK2V8'><fieldset id='bK2V8'></fieldset></dl></div>

    <small id='bK2V8'></small><noframes id='bK2V8'>

  2. <legend id='bK2V8'><style id='bK2V8'><dir id='bK2V8'><q id='bK2V8'></q></dir></style></legend>

      使用 JavaScript 和 Canvas 实现 ColorPicker

      ColorPicker implementation using JavaScript and Canvas(使用 JavaScript 和 Canvas 实现 ColorPicker)
      1. <tfoot id='v3Aut'></tfoot>
        <legend id='v3Aut'><style id='v3Aut'><dir id='v3Aut'><q id='v3Aut'></q></dir></style></legend>

        • <i id='v3Aut'><tr id='v3Aut'><dt id='v3Aut'><q id='v3Aut'><span id='v3Aut'><b id='v3Aut'><form id='v3Aut'><ins id='v3Aut'></ins><ul id='v3Aut'></ul><sub id='v3Aut'></sub></form><legend id='v3Aut'></legend><bdo id='v3Aut'><pre id='v3Aut'><center id='v3Aut'></center></pre></bdo></b><th id='v3Aut'></th></span></q></dt></tr></i><div id='v3Aut'><tfoot id='v3Aut'></tfoot><dl id='v3Aut'><fieldset id='v3Aut'></fieldset></dl></div>
            <bdo id='v3Aut'></bdo><ul id='v3Aut'></ul>

              <small id='v3Aut'></small><noframes id='v3Aut'>

                  <tbody id='v3Aut'></tbody>

              • 本文介绍了使用 JavaScript 和 Canvas 实现 ColorPicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我尝试使用 Canvas 实现 ColorPicker 只是为了好玩.但我似乎迷路了.因为我的浏览器由于所有这些 for 循环而在加载时冻结了一段时间.我正在添加此脚本结果的屏幕截图:

                I'm trying to implement ColorPicker using Canvas just for fun. But i seem lost. as my browser is freezing for a while when it loads due to all these for loops. I'm adding the screenshot of the result of this script:

                目前,我只想用更好的算法解决冻结问题,并且不显示黑色和灰色.请有人帮助我.

                Currently , i only want a solution about the freezing problem with better algorithm and it's not displaying the BLACK and GREY colors. Please someone help me.

                推荐答案

                如果要获取鼠标下像素的rgba,必须使用context.getImageData.

                If you want to fetch the rgba of the pixel under the mouse, you must use context.getImageData.

                getImageData 返回一个像素数组.

                getImageData returns an array of pixels.

                var pixeldata=context.getImageData(0,0,canvas.width,canvas.height);
                

                每个像素由 4 个连续的数组元素定义.

                Each pixel is defined by 4 sequential array elements.

                所以如果你用 getImageData 得到了一个像素数组:

                So if you have gotten a pixel array with getImageData:

                // first pixel defined by the first 4 pixel array elements
                
                pixeldata[0]  =  red component of pixel#1
                pixeldata[1]  =  green component of pixel#1
                pixeldata[2]  =  blue component of pixel#1
                pixeldata[4]  =  alpha (opacity) component of pixel#1
                
                // second pixel defined by the next 4 pixel array elements
                
                
                pixeldata[5]  =  red component of pixel#2
                pixeldata[6]  =  green component of pixel#2
                pixeldata[7]  =  blue component of pixel#2
                pixeldata[8]  =  alpha (opacity) component of pixel#2
                

                所以如果你有一个 mouseX 和 mouseY 那么你可以像这样获得鼠标下的 r,g,b,a 值:

                So if you have a mouseX and mouseY then you can get the r,g,b,a values under the mouse like this:

                // get the offset in the array where mouseX,mouseY begin
                
                var offset=(imageWidth*mouseY+mouseX)*4;
                
                // read the red,blue,green and alpha values of that pixel
                
                var red =   pixeldata[offset];
                var green = pixeldata[offset+1];
                var blue =  pixeldata[offset+2];
                var alpha = pixeldata[offset+3];
                

                这是一个在画布上绘制色轮并在鼠标下显示 RGBA 的演示:

                Here's a demo that draws a colorwheel on the canvas and displays the RGBA under the mouse:

                http://jsfiddle.net/m1erickson/94BAQ/

                这篇关于使用 JavaScript 和 Canvas 实现 ColorPicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)

                      <tbody id='22FMr'></tbody>

                  • <tfoot id='22FMr'></tfoot>

                    • <small id='22FMr'></small><noframes id='22FMr'>

                        • <bdo id='22FMr'></bdo><ul id='22FMr'></ul>
                          <i id='22FMr'><tr id='22FMr'><dt id='22FMr'><q id='22FMr'><span id='22FMr'><b id='22FMr'><form id='22FMr'><ins id='22FMr'></ins><ul id='22FMr'></ul><sub id='22FMr'></sub></form><legend id='22FMr'></legend><bdo id='22FMr'><pre id='22FMr'><center id='22FMr'></center></pre></bdo></b><th id='22FMr'></th></span></q></dt></tr></i><div id='22FMr'><tfoot id='22FMr'></tfoot><dl id='22FMr'><fieldset id='22FMr'></fieldset></dl></div>

                          <legend id='22FMr'><style id='22FMr'><dir id='22FMr'><q id='22FMr'></q></dir></style></legend>