英文:
How to add gradient to strokeStyle canvas in javascript
问题
如何在JavaScript的canvas中的strokeStyle添加渐变颜色。
请帮助我,我的代码运行得很完美,但唯一的问题是我一直在尝试将渐变颜色添加到stroke样式,但它总是破坏我的代码。
有谁能帮助解决这个关于渐变颜色到strokeStyle的问题
HTML
<div class="countItem minutes">
<canvas id="minutes-canvas" width="200" height="200"></canvas>
<svg width="100%" height="100%">
<circle id="outer" cx="50%" cy="50%" r="45%" fill="transparent" stroke-width="1%" stroke="#fff" />
</svg>
<h3>
<span id="minutes-value"></span><br>
<small>minutes</small>
</h3>
</div>
JAVASCRIPT
// 设置发射日期(毫秒)
const launchDate = new Date("May 7, 2020 00:00").getTime();
// 上下文对象
const c = {
context: {},
values: {},
times: {}
};
// 将弧度转换为度数
function deg(d) {
return (Math.PI / 180) * d - (Math.PI / 180) * 90;
}
function render() {
c.context.minutes.clearRect(0, 0, 200, 200);
c.context.minutes.beginPath();
// 创建渐变
var gradient = c.context.minutes.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0, "#bbee2b");
gradient.addColorStop(1, "#ff0000");
c.context.minutes.strokeStyle = gradient;
c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
c.context.minutes.lineWidth = 12;
c.context.minutes.lineCap = "round";
c.context.minutes.stroke();
}
function init() {
// 获取2D上下文
c.context.minutes = document.getElementById('minutes-canvas').getContext('2d');
// 获取显示的值
c.values.minutes = document.getElementById('minutes-value');
setInterval(function () {
// 获取今天的日期和时间(毫秒)
const now = new Date().getTime();
// 获取从现在到发射日期的距离
const distance = launchDate - now;
// 时间计算
c.times.minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
c.values.minutes.innerText = c.times.minutes;
render(); // 绘制!
}, 1000);
}
init();
英文:
How do I add gradient color to strokeStyle canvas in javascript.
Please help me my code works perfect but problem is only that I've been trying to add gradient color to a stroke style but it just breaks my code.
Any one who can help on this Gradient color to strokeStyle
<br>
HTML
<div class="countItem minutes">
<canvas id="minutes-canvas" width="200" height="200"></canvas>
<svg width="100%" height="100%">
<circle id="outer" cx="50%" cy="50%" r="45%" fill="transparent" stroke-width="1%" stroke="#fff" />
</svg>
<h3>
<span id="minutes-value"></span><br>
<small>minutes</small>
</h3>
</div>
JAVASCRIPT
// Set Launch Date (ms)
const launchDate = new Date("May 7, 2020 00:00").getTime();
// Context object
const c = {
context: {},
values: {},
times: {}
};
// Convert radians to degrees
function deg(d) {
return (Math.PI / 180) * d - (Math.PI / 180) * 90;
}
function render() {
c.context.minutes.clearRect(0, 0, 200, 200);
c.context.minutes.beginPath();
c.context.minutes.strokeStyle = "#bbee2b";
c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
c.context.minutes.lineWidth = 12;
c.context.minutes.lineCap = "round";
c.context.minutes.stroke();
}
function init() {
// Get 2D contexts
c.context.minutes = document.getElementById('minutes-canvas').getContext('2d');
// Get displayed values
c.values.minutes = document.getElementById('minutes-value');
setInterval(function () {
// Get todays date and time (ms)
const now = new Date().getTime();
// Get distance from now to launchDate
const distance = launchDate - now;
// Time calculations
c.times.minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
c.values.minutes.innerText = c.times.minutes;
render(); // Draw!
}, 1000);
}
init();
答案1
得分: 3
你可以通过调用 CanvasRenderingContext2D
的 createLinearGradient
或 createRadialGradient
方法来创建一个 CanvasGradient
。创建渐变后,你可以通过调用 addColorStop(offset, color)
方法为其添加颜色节点。
在你的代码中,你将 CanvasRenderingContext2D
存储在 c.context.minutes
中,因此你可以按照以下方式进行操作:
function render() {
c.context.minutes.clearRect(0, 0, 200, 200);
c.context.minutes.beginPath();
const gradient = c.context.minutes.createLinearGradient(0, 0, 200, 200);
gradient.addColorStop(0, 'red');
gradient.addColorStop(0.5, 'blue');
gradient.addColorStop(1, 'green');
c.context.minutes.strokeStyle = gradient;
c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
c.context.minutes.lineWidth = 12;
c.context.minutes.lineCap = "round";
c.context.minutes.stroke();
}
参考链接:https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient
英文:
You can create a CanvasGradient
by calling the methods createLinearGradient
or createRadialGradient
of your CanvasRenderingContext2D
. After creating a gradient you add color stops to it by calling the method addColorStop(offset, color)
.
In your code you are storing the CanvasRenderingContext2D
in c.context.minutes
, so you can do something along these lines:
function render() {
c.context.minutes.clearRect(0, 0, 200, 200);
c.context.minutes.beginPath();
const gradient = c.context.minutes.createLinearGradient(0,0, 200,200);
gradient.addColorStop(0, 'red');
gradient.addColorStop(.5, 'blue');
gradient.addColorStop(1, 'green');
c.context.minutes.strokeStyle = gradient;
c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
c.context.minutes.lineWidth = 12;
c.context.minutes.lineCap = "round";
c.context.minutes.stroke();
}
References: https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient
答案2
得分: 2
var gradient = document.getElementById('minutes-canvas').getContext('2d').createLinearGradient(0, 0, 0, 170);
gradient.addColorStop(0, '#05a');
gradient.addColorStop(1, '#0a5');
// Set Launch Date (ms)
const launchDate = new Date("May 7, 2020 00:00").getTime();
// Context object
const c = {
context: {},
values: {},
times: {}
};
// Convert radians to degrees
function deg(d) {
return (Math.PI / 180) * d - (Math.PI / 180) * 90;
}
function render() {
var gradient = document.getElementById('minutes-canvas').getContext('2d').createLinearGradient(0, 0, 0, 170);
gradient.addColorStop(0, '#05a');
gradient.addColorStop(1, '#0a5');
c.context.minutes.clearRect(0, 0, 200, 200);
c.context.minutes.beginPath();
c.context.minutes.strokeStyle = gradient;
c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
c.context.minutes.lineWidth = 12;
c.context.minutes.lineCap = "round";
c.context.minutes.stroke();
}
function init() {
// Get 2D contexts
c.context.minutes = document.getElementById('minutes-canvas').getContext('2d');
// Get displayed values
c.values.minutes = document.getElementById('minutes-value');
setInterval(function() {
// Get todays date and time (ms)
const now = new Date().getTime();
// Get distance from now to launchDate
const distance = launchDate - now;
// Time calculations
c.times.minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
c.values.minutes.innerText = c.times.minutes;
render(); // Draw!
}, 1000);
}
init();
<div class="countItem minutes">
<canvas id="minutes-canvas" width="200" height="200"></canvas>
<svg width="100%" height="100%">
<circle id="outer" cx="50%" cy="50%" r="45%" fill="transparent" stroke-width="1%" stroke="#fff" />
</svg>
<h3>
<span id="minutes-value"></span><br>
<small>minutes</small>
</h3>
</div>
英文:
You can create a gradient and assign that to stroke
var gradient = document.getElementById('minutes-canvas').getContext('2d').createLinearGradient(0, 0, 0, 170);
gradient.addColorStop(0, '#05a');
gradient.addColorStop(1, '#0a5');
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Set Launch Date (ms)
const launchDate = new Date("May 7, 2020 00:00").getTime();
// Context object
const c = {
context: {},
values: {},
times: {}
};
// Convert radians to degrees
function deg(d) {
return (Math.PI / 180) * d - (Math.PI / 180) * 90;
}
function render() {
var gradient = document.getElementById('minutes-canvas').getContext('2d').createLinearGradient(0, 0, 0, 170);
gradient.addColorStop(0, '#05a');
gradient.addColorStop(1, '#0a5');
c.context.minutes.clearRect(0, 0, 200, 200);
c.context.minutes.beginPath();
c.context.minutes.strokeStyle = gradient;
c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
c.context.minutes.lineWidth = 12;
c.context.minutes.lineCap = "round";
c.context.minutes.stroke();
}
function init() {
// Get 2D contexts
c.context.minutes = document.getElementById('minutes-canvas').getContext('2d');
// Get displayed values
c.values.minutes = document.getElementById('minutes-value');
setInterval(function() {
// Get todays date and time (ms)
const now = new Date().getTime();
// Get distance from now to launchDate
const distance = launchDate - now;
// Time calculations
c.times.minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
c.values.minutes.innerText = c.times.minutes;
render(); // Draw!
}, 1000);
}
init();
<!-- language: lang-html -->
<div class="countItem minutes">
<canvas id="minutes-canvas" width="200" height="200"></canvas>
<svg width="100%" height="100%">
<circle id="outer" cx="50%" cy="50%" r="45%" fill="transparent" stroke-width="1%" stroke="#fff" />
</svg>
<h3>
<span id="minutes-value"></span><br>
<small>minutes</small>
</h3>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论