Примечание: Этот ответ является поздним и большим по сравнению с оригинальным.После первой попытки, когда я неправильно понял намерения ОП, ОП добавил ссылку с примером, с тех пор у меня было намерение предложить решение, так как я не был удовлетворен текущими ответами, и упражнение было забавным.
Разбивка ответа состоит из двух основных частей:
- Смешивание цветов (уже есть ответы, которые занимаются этим, с использованием библиотеки или создания метода для интерполяции / смешивания).шестнадцатеричное значение цвета)
- Нарисуйте дугу и используйте смешанный цвет
/*
* blending colors
* (borrowed and modified from @mark-meyer answer)
*/
// break hex integer into components:
const getComponents = (color) => Array.from({length: 3}, (_, i) => Math.floor(color / 16 ** (2 * i) % 16 ** 2))
// interpolate arrays by component
const interpolate = (arr1, arr2, percent) => arr1.map((v, index) => Math.floor(v + (arr2[index] - v) * (percent / 100)))
const colorBlend = value => {
const StartColor = 0x11FF11; // Green
const MiddleColor = 0xFFA726; // Orange
const EndColor = 0xEF5350; // Red
let [start, end, v] = value < 50
? [StartColor, MiddleColor, value * 2 ]
: [MiddleColor, EndColor, (value - 50) * 2]
let interpoled = interpolate(getComponents(start), getComponents(end), v)
const color = interpoled
.reduce((n, component, index) => n + component * (16 ** (index * 2)), 0)
.toString(16).padStart(6, '0')
return `#${color}`
}
/*
* draw an arc
*/
const canvas = document.getElementById('canvas1')
const ctx = canvas.getContext('2d')
const size = 30
let p = 0
const grad = ctx.createLinearGradient(size, size, size * 2, size * 2)
grad.addColorStop(0, "#66BB6A")
grad.addColorStop(0.5, "#FFA726")
grad.addColorStop(1, "#EF5350")
ctx.lineWidth = 6
const draw = () => {
p += 0.01
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.strokeStyle = colorBlend(p * 50)
ctx.beginPath()
ctx.arc(size * 1.5, size * 1.5, size, 1.5 * Math.PI, (1.5 + p) * Math.PI)
ctx.stroke()
if (p < 2) {
window.requestAnimationFrame(draw);
}
}
window.requestAnimationFrame(draw);
<canvas id="canvas1" width="500" height="500"></canvas>