创建调色板<img/>或<div>或<canvas>?</canvas> </div>

时间:2012-03-21 03:15:55

标签: html semantics

我目前正在使用HTML,CSS和Javascript处理颜色选择器组件,一个主要问题是我应该如何实现这样的调色板:

enter image description here

忽略IE8或更早版本引入的浏览器兼容性问题,我有3种方法来实现它:

  1. <img>方式:

    这很简单,多年来被许多颜色选择器组件使用,我们只使用<img src="palette.png" />包含与上述图像完全相同的图像。

    问题是<img>会引入额外的网络往返,虽然我们可以使用dataURI阻止它,但图像数据对我来说有点太大

  2. <div>方式:

    这样我们应该使用2个<div>元素,其中一个提供从左到右的线性渐变,另一个提供从上到下的alpha渐变,样式应为:

    <style>
        #map {
            width: 400px; 
            height: 400px;
            background: -webkit-gradient(
                linear, left top, right top, 
                color-stop(0%,#ff0000), 
                color-stop(16.67%,#ffff00), 
                color-stop(33.33%,#00ff00),
                color-stop(50%,#00ffff), 
                color-stop(66.67%,#0000ff),
                color-stop(83.33%,#ff00ff), 
                color-stop(100%,#ff0000)
            );
        }
        #overlay {
            width: 400px;
            height: 400px;
            background: -webkit-gradient(
                linear, left top, left bottom, 
                from(rgba(0, 0, 0, 0)), 
                to(rgba(0, 0, 0, 1))
            );
        }
    </style>
    
    <div id="palette"><div id="overlay"></div></div>
    

    它工作得很好,但问题是,这个方法仅引入2个元素用于样式,2个<div>元素没有任何语义含义,并且不作为通用容器或块,这是违反语义HTML

  3. <canvas>方式:

    <canvas>提供了非常灵活的图形API来实现这样的调色板,我使用这些代码来绘制它:

    var context = canvas.getContext('2d');
    
    var palette = context.createLinearGradient(0, 0, 360, 0);
    palette.addColorStop(0 / 6, '#ff0000');
    palette.addColorStop(1 / 6, '#ffff00');
    palette.addColorStop(2 / 6, '#00ff00');
    palette.addColorStop(3 / 6, '#00ffff');
    palette.addColorStop(4 / 6, '#0000ff');
    palette.addColorStop(5 / 6, '#ff00ff');
    palette.addColorStop(6 / 6, '#ff0000');
    context.fillStyle = palette;
    context.fillRect(0, 0, 360, 360);
    
    var overlay = context.createLinearGradient(0, 0, 0, 360);
    overlay.addColorStop(0, 'rgba(0, 0, 0, 0)');
    overlay.addColorStop(1, 'rgba(0, 0, 0, 1)');
    context.fillStyle = overlay;
    context.fillRect(0, 0, 360, 360);
    

    它与上述两种方法一样有效,但我想知道是否应该使用画布绘制这样的静态图像,不应该在更动态和复杂的环境中使用画布吗?

  4. 那么我应该采用哪种方式来实现简单的调色板,还是有更好的方法呢?

3 个答案:

答案 0 :(得分:2)

如果您想使用图片,但又不想要额外的请求,可以使用data URI

<img width="184" height="184" title="" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALgAAAC4CAIAAADfUbGQAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAgAElEQVR4nO2d25LjOnJFU2V7/DAz//+hYz+4PSH4oUqsRO5LJiR19/HEQTCqSFxXJjZAkKKo21or/gx/hi58/G6AP8P/j/DvX/9vt/hLxN/i++9/7ocl9e8s8sr59+/DH3+JHxE/Iv4r4kfEf8d2mP9eqSXyOvxfSAW4v2riv7JIXbC09D8a60fEP6xV//g+/MsP0qry4l817t8i/gNSAe4fh8Si4Pqfb6Ecn37WdFtrnvenhjnFJ/dJ9j8E8VGJg3CLiEso99cx83b/3u73fPRSrT/Bt4z4izt+AfebfPy1QcR7iP8tC2UhvjdrbGueUVR230LLNYNWluTUzC1TXH0nQWV/lhiLToiV02qAGcVXiKqmIk+H14yyTtRuTGGWvQV6Z2yh0QYFzbqj1cE5cbC4O5SbQ2/BrlFMF6lWwEq6Rgm937bcBdVFSx+y3pmIF6t8CloZPml5j0diZbiilNC7UDxy2Z+5/VMoKouqGw3S1hhoWlOr1YdQTBZkVU11/C0xdTPVTaOinh4pvoNYzCpZ0G0/13zviMVsOTuVHdUdRjcddAhWDR0z6GGPvEa8dPt2MXsEXbxOgljMDne8jCJixVokY9nxddM8L7N2O7Tjj8wQpM+Bhk7aO7llnaiYZNCXx6WUUeF9/1sWhbAuRHnfwQj00SxM9FvaZN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WBXfQWM2DNqMpXGxMo/YzVxlzrjtSXQQCOJCM/Gx9msK3Xtmr4BcZkDCyKRGPBfZBSpbnzn2v4nbq9WAzqCpJo5y7txv8TFHeOqzHhqJk83Oougo4FgWnhL5/CFUNoQ2lghj5oiRIrswdPAz0CevOG/xrcPNDsZL3mmgHRGsR2wdBhrhBvqYEJfDc+LJzrwHvoJYzAajQ0yloVvN7518mioIAjTRQtsiryBqyRefIRCGtshty1v232PG4YwSmjSrBKqjOC2vytAFAxq78G3FLZPiU/ushYlrA9ysp5bnQHvv6l8Ao87ExawqGJvOkcU782RSbzlUaC08PLmc64NC3KAmzJwzgGje5dotafwoZMln3UtjWmSF4S0DSpiMa4xx8lPQE8ROtH6gnRA/DdQgirdCKkwVOpUMcV5INWqYVHlyGnpHqpJzG8bEPvGYeHwLv63q0OiTNcc88zoh/gwDh59znNZHoX8tcZN/9lZIDLR/5DsAAAAqSURBVGjZKdJJa++o41RA72jztQr+UMS3td7R7p/hXz189Fn+DH+GiP8Dub68wWaFHokAAAAASUVORK5CYII=" />

Demo

答案 1 :(得分:0)

调色板的目的是什么?

它只是显示?那么也许一个图像最好用,因为它可以被缓存和压缩。

您打算使用调色板选择颜色吗?然后我会选择画布,因为很容易找到像素的颜色。

答案 2 :(得分:0)

额外的图片请求真的并不是那么糟糕;和bookcasey said一样,你总是可以嵌入它。

我会选择<canvas>。使用<div>没有什么特别的错误,但如果用户正在使用CSS浏览它将无法正常工作。 (是的,我们中的一些人确实浏览了CSS)

<canvas>不仅适用于CSS,它更具语义性,而<divs>对于屏幕阅读器或搜索引擎来说似乎毫无意义。

此外,从长期来看,<canvas>将允许你更多更多的灵活性,JS apis等等。

例如,使用画布,您可以实现多种方式来选择颜色(使用几种color models中的任意一种来选择颜色。

使用<div>或图像,这个额外的功能会很快阻塞DOM或您的服务器使用HTTP请求,但使用Canvas,您可以获得更多的灵活性和最少的所需资源