发布于2022-07-05 04:16 阅读(802) 评论(0) 点赞(8) 收藏(2)
我想用画布实现这样的目标
我想要实现的是我可以像这样生成色轮
$("#id").colorWheel({
width: 200,
height: 200
});
我已经尝试用谷歌搜索它,但我找不到完美的色轮选择器,我只需要没有任何滑块的色轮,我已经尝试了 iro js,但我无法从插件中删除滑块,谁能帮忙我用 javascript/Jquery 和画布创建这个色轮?
codereview.stackexchange.com 上有这个问题的答案:https ://codereview.stackexchange.com/questions/69887/drawing-a-color-wheel-faster
只是为了好玩,这里是一个实现:
/**
* degreesToRadians
*
* @param {number} degrees
* @returns {number} radians
*/
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180);
}
/**
* generateColorWheel
*
* @param {number} [size=400]
* @param {string} [centerColor="white"]
* @returns {HTMLCanvasElement}
*/
function generateColorWheel(size, centerColor) {
if (size === void 0) { size = 400; }
if (centerColor === void 0) { centerColor = "white"; }
//Generate main canvas to return
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = canvas.height = size;
//Generate canvas clone to draw increments on
var canvasClone = document.createElement("canvas");
canvasClone.width = canvasClone.height = size;
var canvasCloneCtx = canvasClone.getContext("2d");
//Initiate variables
var angle = 0;
var hexCode = [255, 0, 0];
var pivotPointer = 0;
var colorOffsetByDegree = 4.322;
//For each degree in circle, perform operation
while (angle++ < 360) {
//find index immediately before and after our pivot
var pivotPointerbefore = (pivotPointer + 3 - 1) % 3;
var pivotPointerAfter = (pivotPointer + 3 + 1) % 3;
//Modify colors
if (hexCode[pivotPointer] < 255) {
//If main points isn't full, add to main pointer
hexCode[pivotPointer] = (hexCode[pivotPointer] + colorOffsetByDegree > 255 ? 255 : hexCode[pivotPointer] + colorOffsetByDegree);
}
else if (hexCode[pivotPointerbefore] > 0) {
//If color before main isn't zero, subtract
hexCode[pivotPointerbefore] = (hexCode[pivotPointerbefore] > colorOffsetByDegree ? hexCode[pivotPointerbefore] - colorOffsetByDegree : 0);
}
else if (hexCode[pivotPointer] >= 255) {
//If main color is full, move pivot
hexCode[pivotPointer] = 255;
pivotPointer = (pivotPointer + 1) % 3;
}
//clear clone
canvasCloneCtx.clearRect(0, 0, size, size);
//Generate gradient and set as fillstyle
var grad = canvasCloneCtx.createRadialGradient(size / 2, size / 2, 0, size / 2, size / 2, size / 2);
grad.addColorStop(0, centerColor);
grad.addColorStop(1, "rgb(" + hexCode.map(function (h) { return Math.floor(h); }).join(",") + ")");
canvasCloneCtx.fillStyle = grad;
//draw full circle with new gradient
canvasCloneCtx.globalCompositeOperation = "source-over";
canvasCloneCtx.beginPath();
canvasCloneCtx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2);
canvasCloneCtx.closePath();
canvasCloneCtx.fill();
//Switch to "Erase mode"
canvasCloneCtx.globalCompositeOperation = "destination-out";
//Carve out the piece of the circle we need for this angle
canvasCloneCtx.beginPath();
canvasCloneCtx.arc(size / 2, size / 2, 0, degreesToRadians(angle + 1), degreesToRadians(angle + 1));
canvasCloneCtx.arc(size / 2, size / 2, size / 2 + 1, degreesToRadians(angle + 1), degreesToRadians(angle + 1));
canvasCloneCtx.arc(size / 2, size / 2, size / 2 + 1, degreesToRadians(angle + 1), degreesToRadians(angle - 1));
canvasCloneCtx.arc(size / 2, size / 2, 0, degreesToRadians(angle + 1), degreesToRadians(angle - 1));
canvasCloneCtx.closePath();
canvasCloneCtx.fill();
//Draw carved-put piece on main canvas
ctx.drawImage(canvasClone, 0, 0);
}
//return main canvas
return canvas;
}
//TEST
//Get color wheel canvas
var colorWheel = generateColorWheel(300);
//Add color wheel canvas to document
document.body.appendChild(colorWheel);
//Add ouput field
var p = document.body.appendChild(document.createElement("p"));
/**
* colorWheelMouse
*
* @param {MouseEvent} evt
*/
function colorWheelMouse(evt) {
var ctx = colorWheel.getContext("2d");
var data = ctx.getImageData(evt.offsetX, evt.offsetY, 1, 1);
p.innerHTML = "RGB: " + data.data.slice(0, 3).join(',');
}
//Bind mouse event
colorWheel.onmousemove = colorWheelMouse;
作者:黑洞官方问答小能手
链接:http://www.qianduanheidong.com/blog/article/378729/a08d5036d65b9f8460ed/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!