美文网首页
5.canvas 优化

5.canvas 优化

作者: 琉璃_xin | 来源:发表于2019-04-11 20:14 被阅读0次

1.离屏渲染

const WIDTH = document.documentElement.clientWidth;
const HEIGHT = document.documentElement.clientHeight;
const CANVAS = document.getElementById('canvas');
canvas.setAttribute("width", WIDTH);
canvas.setAttribute("height", HEIGHT);
const ctx = CANVAS.getContext('2d');
const NUM = rand(200, 300);
const SSC = 'white';
let snowArr = [];
const cacheCanvas = document.createElement("canvas");
cacheCanvas.setAttribute("width", WIDTH);
cacheCanvas.setAttribute("height", HEIGHT);
const cacheCtx = cacheCanvas.getContext("2d");

/**
 * 随机值
 * @param {number} rMin 
 * @param {number} rMax 
 */
function rand (rMin, rMax) {
  return ~~((Math.random() * (rMax - rMin) + rMin))
}

/**
 * requestAnimatonFrame兼容
 */
window.requestAnimationFrame = (function() {
  return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    function(cb) {
      window.setTimeout(cb, 1E3 / 60);
    }
})();


/**
 * 单个雪花
 * @param {number} x x位置
 * @param {number} y y位置
 * @param {number} sr 半径
 * @param {number} sx x方向速度
 * @param {number} sy y方向速度
 */
class Snow {
  constructor() {
    this.x = rand(0, WIDTH);
    this.y = rand(0, HEIGHT/ 2);
    this.sr = rand(1,3);
    this.sx = rand(-2,2);
    this.sy = rand(1,4);
  }
  draw() {
      cacheCtx.beginPath();
      cacheCtx.arc(this.x,this.y,this.sr,0,Math.PI * 2,true);
      cacheCtx.fillStyle = '#fff';
      cacheCtx.fill();
      cacheCtx.closePath();
  }
  move() {
    this.x = this.x + this.sx;
    this.y = this.y + this.sy;
    if(this.x < 0 || this.x > CANVAS.width || this.y > CANVAS.height) {
      this.x = rand(0, WIDTH);
      this.y = rand(0, HEIGHT/ 5);
    }
    this.draw()
  }
}

for(let i = 0; i < NUM; i++){
  let snow = new Snow();
  snowArr.push(snow);
}

function init () { 
  cacheCtx.clearRect(0,0,CANVAS.width,CANVAS.height);
  requestAnimationFrame(init)

  snowArr.forEach( (snow) => {
    snow.move();
  });

  //将离屏画布画到真实画布上
  ctx.clearRect(0,0,CANVAS.width,CANVAS.height);
  ctx.drawImage(cacheCanvas,0,0)
}

init();

2.避免浮点数的坐标点,使用整数

ctx.drawImage(myImage, 0.3, 0.5);

此时应使用Math.floor()取整

3.不要在用drawImage时缩放图像

4.关闭透明度

var ctx = canvas.getContext('2d', { alpha: false });

相关文章

  • 5.canvas 优化

    1.离屏渲染 2.避免浮点数的坐标点,使用整数 此时应使用Math.floor()取整 3.不要在用drawIma...

  • 内存优化

    内存优化、UI优化(布局优化、会只优化)、速度优化(线程优化、网络优化)、启动优化、电量优化 内存优化 内存抖动:...

  • Android进阶之性能优化

    一、性能优化分类 布局优化 绘制优化 内存泄漏优化 响应速度优化 ListView优化 Bitmap优化 线程优化...

  • 性能优化

    内容优化 服务器优化 Cookie优化 CSS优化 javascript优化 图像优化

  • Android开发艺术探索之性能优化笔记

    Android性能优化 一,优化内容 布局优化、绘制优化、内存泄漏优化、响应速度优化、ListView优化、Bit...

  • Android性能优化

    Android性能优化包括布局优化、绘制优化、内存优化、线程优化、响应速度优化、Bitmap优化和ListView...

  • 对于手游的优化

    给手游做优化,无非就CPU性能优化、内存性能优化、资源优化、GPU优化、IO优化、网络优化、耗电优化这些,为此汇总...

  • 网站内部优化的流程介绍

    站内优化:网站本身内部的优化,其中包括代码优化、标签优化、内容优化、url优化等; 站内优化的重要性: 站内优化是...

  • 「性能优化系列」APP内存优化理论与实践

    性能优化系列: 启动优化 内存优化 布局优化 卡顿优化 apk瘦身优化 电量优化项目地址: fuusy/F...

  • Android 性能优化

    app性能优化 android优化分为: 内存优化 UI优化 电量优化 apk瘦身优化 启动优化 下面通过各种百度...

网友评论

      本文标题:5.canvas 优化

      本文链接:https://www.haomeiwen.com/subject/lbwxwqtx.html