【问题标题】:HTML5 Canvas + JS Not Working on IOS/SafariHTML5 Canvas + JS 在 IOS/Safari 上不起作用
【发布时间】:2017-05-15 20:39:06
【问题描述】:

我已经实现了一个带有 javascript 粒子效果的画布元素。该效果适用于除 IOS/Safari 之外的所有浏览器。我做了一些研究,IOS/Safari 支持 HTML5/Canvas:According to Browser Support Charts for HTML5/Canvas

$(function() {
  var WIDTH = window.innerWidth * .9,
    HEIGHT = window.innerHeight,
    MAX_PARTICLES = 100,
    DRAW_INTERVAL = 60,
    canvas = document.querySelector('#pixies'),
    context = canvas.getContext('2d'),
    gradient = null,
    pixies = new Array();

  function setDimensions() { 
    WIDTH = window.outerWidth;
    HEIGHT = window.innerHeight;
    canvas.width = WIDTH; 
    canvas.height = HEIGHT; 
  }

  setDimensions();
  window.addEventListener('resize', setDimensions);

  function Circle() {
    this.settings = {ttl:8000, xmax:5, ymax:2, rmax:10, rt:1, xdef:960, ydef:540, xdrift:4, ydrift: 4, random:true, blink:true};

    this.reset = function() {
      this.x = (this.settings.random ? WIDTH*Math.random() : this.settings.xdef);
      this.y = (this.settings.random ? HEIGHT*Math.random() : this.settings.ydef);
      this.r = ((this.settings.rmax-1)*Math.random()) + 1;
      this.dx = (Math.random()*this.settings.xmax) * (Math.random() < .5 ? -1 : 1);
      this.dy = (Math.random()*this.settings.ymax) * (Math.random() < .5 ? -1 : 1);
      this.hl = (this.settings.ttl/DRAW_INTERVAL)*(this.r/this.settings.rmax);
      this.rt = Math.random()*this.hl;
      this.settings.rt = Math.random()+1;
      this.stop = Math.random()*.2+.4;
      this.settings.xdrift *= Math.random() * (Math.random() < .5 ? -1 : 1);
      this.settings.ydrift *= Math.random() * (Math.random() < .5 ? -1 : 1);
    }

    this.fade = function() {
      this.rt += this.settings.rt;
    }

    this.draw = function() {
      if(this.settings.blink && (this.rt <= 0 || this.rt >= this.hl)) {
          this.settings.rt = this.settings.rt*-1;
      } else if(this.rt >= this.hl) {
          this.reset();
      }

      var newo = 1-(this.rt/this.hl);
      context.beginPath();
      context.arc(this.x, this.y, this.r, 0, Math.PI*2, true);
      context.closePath();

      var cr = this.r*newo;
      gradient = context.createRadialGradient(this.x, this.y, 0, this.x, this.y, (cr <= 0 ? 1 : cr));
      gradient.addColorStop(0.0, 'rgba(255,255,255,'+newo+')');
      gradient.addColorStop(this.stop, 'rgba(255,255,255,'+(newo*.6)+')');
      gradient.addColorStop(1.0, 'rgba(255,255,255,0)');
      context.fillStyle = gradient;
      context.fill();
    }

    this.move = function() {
      this.x += (this.rt/this.hl)*this.dx;
      this.y += (this.rt/this.hl)*this.dy;
      if(this.x > WIDTH || this.x < 0) this.dx *= -1;
      if(this.y > HEIGHT || this.y < 0) this.dy *= -1;
    }

    this.getX = function() { return this.x; }
    this.getY = function() { return this.y; }
  }

  for (var i = 0; i < MAX_PARTICLES; i++) {
    pixies.push(new Circle());
    pixies[i].reset();
  }

  function draw() {
    context.clearRect(0, 0, WIDTH, HEIGHT);
    for(var i = 0; i < pixies.length; i++) {
      pixies[i].fade();
      pixies[i].move();
      pixies[i].draw();
    }
  }

  setInterval(draw, DRAW_INTERVAL);
});
#particles {
  position: absolute;
  background: navy;
  width: 200px;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="particles">
   <canvas id="pixies"></canvas>
</div>

任何想法为什么这在 IOS/Safari 中不起作用?有问题的实际站点是这个:www.pjmaskslive.com

【问题讨论】:

  • 实际查看开发人员控制台可能会有所帮助。有什么错误吗?此外,注意 caniuse 网站上的“已知问题”选项卡通常是个好主意!旁注:谁在乎 Safari?
  • 有一些关于 Safari 在 stackoverflow 上不能很好地与 Canvas 配合使用的传言(例如,似乎不支持 .bind())。我在任何地方都没有bind() 的实例。它适用于基于桌面 (OSX) 的 Safari,而不是 IOS/Safari。只是好奇为什么会这样。
  • 上面的 sn-p 在 iOS 10.3.2 上的 Safari 中对我来说运行良好
  • @LennartHase:你介意在 IOS/Safari 上试试这个网站吗? pjmaskslive.com
  • @JoelB 好的,无法在您链接的网站上运行,但是控制台中也没有错误或警告,很好奇...

标签: javascript jquery css html canvas


【解决方案1】:

问题似乎是上述website 上的画布元素宽度设置为0。所以我假设问题是在开始时设置WIDTH 变量。这可能是由于 this 问题中提到的 iOS 10 的持续错误。

另一种方法可能是使用document.body.getBoundingClientRect().width 作为window.innerWidth 和/或window.outerWidth 的替代方法。 screen.width 也可以使用,但是,这些也可能与您以前的方法存在相同的问题。

看起来像是一个 Safari 问题,而不是你的代码!

【讨论】:

  • 我设置了固定宽度,问题依旧,iphone canvas真的不行了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
相关资源
最近更新 更多