性能方面-画布与基本URI与图像
Performance-wise - canvas vs base URI vs image
我正在创建一个色轮(选择器),我想知道显示色轮的最快、最有效的方法。我目前正在使用JavaScript通过画布生成它。我认为其他选项是实际的图像或数据URI。如果有更快的方法,请告诉我。
显示颜色选择器的最快、最有效的方法是什么?
使用JavaScript/画布的色轮
JSFiddle
var colorDisc = document.getElementById('surface'),
colorDiscRadius = colorDisc.offsetHeight / 2;
var drawDisk = function(ctx, coords, radius, steps, colorCallback) {
var x = coords[0] || coords, // coordinate on x-axis
y = coords[1] || coords, // coordinate on y-axis
a = radius[0] || radius, // radius on x-axis
b = radius[1] || radius, // radius on y-axis
angle = 360,
rotate = 0,
coef = Math.PI / 180;
ctx.save();
ctx.translate(x - a, y - b);
ctx.scale(a, b);
steps = (angle / steps) || 360;
for (; angle > 0; angle -= steps) {
ctx.beginPath();
if (steps !== 360) ctx.moveTo(1, 1); // stroke
ctx.arc(1, 1, 1, (angle - (steps / 2) - 1) * coef, (angle + (steps / 2) + 1) * coef);
if (colorCallback) {
colorCallback(ctx, angle);
} else {
ctx.fillStyle = 'black';
ctx.fill();
}
}
ctx.restore();
},
drawCircle = function(ctx, coords, radius, color, width) { // uses drawDisk
width = width || 1;
radius = [
(radius[0] || radius) - width / 2, (radius[1] || radius) - width / 2
];
drawDisk(ctx, coords, radius, 1, function(ctx, angle) {
ctx.restore();
ctx.lineWidth = width;
ctx.strokeStyle = color || '#000';
ctx.stroke();
});
};
if (colorDisc.getContext) {
drawDisk( // HSV color wheel with white center
colorDisc.getContext("2d"), [colorDisc.width / 2, colorDisc.height / 2], [colorDisc.width / 2 - 1, colorDisc.height / 2 - 1],
360,
function(ctx, angle) {
var gradient = ctx.createRadialGradient(1, 1, 1, 1, 1, 0);
gradient.addColorStop(0, 'hsl(' + (360 - angle + 0) + ', 100%, 50%)');
gradient.addColorStop(1, "#FFFFFF");
ctx.fillStyle = gradient;
ctx.fill();
}
);
drawCircle( // gray border
colorDisc.getContext("2d"), [colorDisc.width / 2, colorDisc.height / 2], [colorDisc.width / 2, colorDisc.height / 2],
'#555',
3
);
}
<canvas id="surface" width="500" height="500"></canvas>
我认为图像会更快,但如果不获得各种缩放伪影,则很难调整其大小。所以我会选择帆布。
然而,我认为还有第三个选项值得考虑:CSS中的角度梯度。以下是使用现有功能的方法:https://css-tricks.com/conical-gradients-css/
相关文章:
- 如何在Edge中下载图像/png数据URI
- canvas没有从uri中绘制完整的图像
- 如何从 base64 数据 URI 在服务器端保存 PNG 图像
- 如何嵌入带有数据uri的大图像
- 性能方面-画布与基本URI与图像
- 将 png 图像从 JavaScript 传递到 PHP(错误:请求 URI 太大)
- 带有 base64 字符串(数据 URI)的 HTML 图像标记
- 如何从 Web API 发布和获取数据 uri 图像
- 科尔多瓦相机插件在从具有文件 URI 的图库中选择时始终返回相同的图像
- 无法将图像数据 URI 转换为像素数据数组
- 使用数据URI快速更新图像会导致缓存、内存泄漏
- 使用数据URI无法显示多个图像:静默浏览器错误
- 是否可以加载图像,在内存中缩放它并将其分配为背景图像的 uri
- Firefox可以't在画布上绘制带有数据uri的图像:NS_ERROR_NOT_AVAILABLE
- 如何确定图像是URI还是JavaScript中的缩略图
- 函数,返回Javascript中图像的数据URI Base64字符串
- 如何将html转换为数据图像uri
- 使用cordova-plugin-camera将URI图像转换为base64格式
- 使用 javascript 将数据 URI 图像上传到 Facebook
- 如何在 Pinterest 上共享数据 uri 图像