如何在JavaScript中将渐变添加到canvas的strokeStyle?

huangapple go评论64阅读模式
英文:

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

      &lt;div class=&quot;countItem minutes&quot;&gt;
        &lt;canvas id=&quot;minutes-canvas&quot; width=&quot;200&quot; height=&quot;200&quot;&gt;&lt;/canvas&gt;
        &lt;svg width=&quot;100%&quot; height=&quot;100%&quot;&gt;
            &lt;circle id=&quot;outer&quot; cx=&quot;50%&quot; cy=&quot;50%&quot; r=&quot;45%&quot; fill=&quot;transparent&quot; stroke-width=&quot;1%&quot; stroke=&quot;#fff&quot; /&gt;
        &lt;/svg&gt;
        &lt;h3&gt;
            &lt;span id=&quot;minutes-value&quot;&gt;&lt;/span&gt;&lt;br&gt;
            &lt;small&gt;minutes&lt;/small&gt;
        &lt;/h3&gt;
    &lt;/div&gt;

JAVASCRIPT

// Set Launch Date (ms)
const launchDate = new Date(&quot;May 7, 2020 00:00&quot;).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 = &quot;#bbee2b&quot;;
    c.context.minutes.arc(100, 100, 90, deg(0), deg(6 * (c.times.minutes - 60)));
    c.context.minutes.lineWidth = 12;
    c.context.minutes.lineCap = &quot;round&quot;;
    c.context.minutes.stroke();

}

function init() {
    // Get 2D contexts
    c.context.minutes = document.getElementById(&#39;minutes-canvas&#39;).getContext(&#39;2d&#39;);

    // Get displayed values
    c.values.minutes = document.getElementById(&#39;minutes-value&#39;);

    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

你可以通过调用 CanvasRenderingContext2DcreateLinearGradientcreateRadialGradient 方法来创建一个 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, &#39;red&#39;);
    gradient.addColorStop(.5, &#39;blue&#39;);
    gradient.addColorStop(1, &#39;green&#39;);
    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 = &quot;round&quot;;
    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(&#39;minutes-canvas&#39;).getContext(&#39;2d&#39;).createLinearGradient(0, 0, 0, 170);
gradient.addColorStop(0, &#39;#05a&#39;);
gradient.addColorStop(1, &#39;#0a5&#39;);

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Set Launch Date (ms)
const launchDate = new Date(&quot;May 7, 2020 00:00&quot;).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(&#39;minutes-canvas&#39;).getContext(&#39;2d&#39;).createLinearGradient(0, 0, 0, 170);
gradient.addColorStop(0, &#39;#05a&#39;);
gradient.addColorStop(1, &#39;#0a5&#39;);
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 = &quot;round&quot;;
c.context.minutes.stroke();
}
function init() {
// Get 2D contexts
c.context.minutes = document.getElementById(&#39;minutes-canvas&#39;).getContext(&#39;2d&#39;);
// Get displayed values
c.values.minutes = document.getElementById(&#39;minutes-value&#39;);
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 -->

&lt;div class=&quot;countItem minutes&quot;&gt;
&lt;canvas id=&quot;minutes-canvas&quot; width=&quot;200&quot; height=&quot;200&quot;&gt;&lt;/canvas&gt;
&lt;svg width=&quot;100%&quot; height=&quot;100%&quot;&gt;
&lt;circle id=&quot;outer&quot; cx=&quot;50%&quot; cy=&quot;50%&quot; r=&quot;45%&quot; fill=&quot;transparent&quot; stroke-width=&quot;1%&quot; stroke=&quot;#fff&quot; /&gt;
&lt;/svg&gt;
&lt;h3&gt;
&lt;span id=&quot;minutes-value&quot;&gt;&lt;/span&gt;&lt;br&gt;
&lt;small&gt;minutes&lt;/small&gt;
&lt;/h3&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年4月9日 21:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61122649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定