javascript - 错误的 Canvas 上下文

标签 javascript html canvas

我目前正在制作 Canvas 动画,效果非常好。

您可以查看下面我的代码:

var canvas = document.getElementById('bubble'),
    ctx = canvas.getContext('2d');
ctx.save();

var wW = canvas.width = window.innerWidth,
    wH = canvas.height = window.innerHeight;

var particles = [];
var particleIndex = 0;
var dx = dy = distance = 0;

function Particle(){
  this.x = Math.random() * wW;
  this.y = Math.random() * wH;
  this.rad = 20;
  this.color = "blue";
  this.vX =  Math.random() * (1.01 - (-1)) + (-1);
  this.vY =  Math.random() * (1.01 - (-1)) + (-1);
  particles[particleIndex] = this;
  particleIndex++;
}
    
Particle.prototype.draw = function(){
  this.x += this.vX;
  this.y += this.vY;
  
  this.collision();
  
  //outer
  ctx.beginPath();
  ctx.arc(this.x, this.y, this.rad, 0, Math.PI * 2);
  ctx.stroke();
  
  //center
  ctx.beginPath();
  ctx.arc(this.x, this.y, this.rad/10, 0, Math.PI * 2);
  ctx.fillStyle="red";
  ctx.fill();
}

Particle.prototype.collision = function(){
 if(this.x + this.rad >= wW || this.x - this.rad <= 0){
   this.vX *= -1;
 }
  
 if(this.y + this.rad >= wH || this.y - this.rad <= 0){
   this.vY *= -1;
 }
 
}

function line(){
  for(var i=0; i < particles.length; i++){
    for(var j=0; j < particles.length; j++){
      dx = particles[i].x - particles[j].x;
      dy = particles[i].y - particles[j].y;
      distance = Math.sqrt(dx * dx + dy * dy);

      if(distance <= 60){        
         ctx.beginPath();
         ctx.moveTo(particles[i].x, particles[i].y);
         ctx.lineTo(particles[j].x, particles[j].y);
         ctx.strokeStyle="rgba(0,0,0,"+ 6/distance +")";
         ctx.stroke();
        }
    }
  }
}
  
for(var i=0; i<20; i++){
  new Particle();
}

function anim(){
  requestAnimationFrame(anim);
  ctx.clearRect(0,0,wW,wH);
  
  line();
  
  for(var i=0; i < particles.length; i++){
    particles[i].draw();
  }
}

anim();
html, body{
  padding:0;
  margin:0;
}

body{
  height: 100%;
  width: 100%;
  overflow:hidden;
}
<canvas id="bubble"></canvas>

我有一个函数 line() ,它计算每个圆之间的空间并在它们之间画线。 在这个函数中,我把这个:

 ctx.strokeStyle="rgba(0,0,0,"+ 6/distance +")";

我想根据距离来改变线条的不透明度。但是,它改变了圆圈的不透明度,而不是线条的透明度。我不明白为什么,我尝试恢复上下文但没有成功..

谢谢!

最佳答案

您可以在开始绘制线条之前将其存储到变量中,然后再将其恢复

function line() {
  var originalStrokeStyle = ctx.strokeStyle;
  for (var i = 0; i < particles.length; i++) {
    for (var j = 0; j < particles.length; j++) {
      dx = particles[i].x - particles[j].x;
      dy = particles[i].y - particles[j].y;
      distance = Math.sqrt(dx * dx + dy * dy);

        if (distance <= 60) {
          ctx.beginPath();
          ctx.moveTo(particles[i].x, particles[i].y);
          ctx.lineTo(particles[j].x, particles[j].y);
          ctx.strokeStyle = "rgba(0,0,0," + 6 / distance + ")";
          ctx.stroke();
        }
    }
  }
  ctx.strokeStyle = originalStrokeStyle;
}

更新了演示:http://codepen.io/gpetrioli/pen/aBZpXJ

关于javascript - 错误的 Canvas 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40596129/

相关文章:

javascript - 谷歌 : "EcmaScript 5 getters and setters for properties are discouraged" Why?

javascript - YouTube API加载不一致

html - 使用 HTML 文档类型在 XHTML 中编写网页的某些部分有什么问题吗?

javascript - 为什么我在调试器中可以看到数据时 window.localStorage 是空的?

java - 如何在 onDraw 方法 Android 之外访问 Canvas 宽度

JavaScript:根据输入有效地创建空的二维数组

Javascript:比较 Protractor 中的值

javascript - 提交 ajax 表单并停留在同一页面上不起作用

javascript - 将另一个函数添加到 onclick

javascript - Canvas 图像。 Base64 不显示