首页 > 软件开发 > HTML >

html5 canvas海底水草动画特效

来源:互联网 2023-03-16 19:11:05 388

html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

工具/原料

  • adobe dreamweaver

方法/步骤

  • 1

    新建html文档。851办公区 - 实用经验教程分享!

    html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

  • 2

    书写hmtl代码。851办公区 - 实用经验教程分享!

    canvas>/canvas>851办公区 - 实用经验教程分享!

    div>html5 canvas海底水草动画特效/div>851办公区 - 实用经验教程分享!

    div>只兼容高版本浏览器 IE8以下无效/div>851办公区 - 实用经验教程分享!

    html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

  • 3

    书写css代码。851办公区 - 实用经验教程分享!

    style>851办公区 - 实用经验教程分享!

    * {851办公区 - 实用经验教程分享!

    margin:0;851办公区 - 实用经验教程分享!

    padding:0;851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    canvas {851办公区 - 实用经验教程分享!

    display:block;851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    /style>851办公区 - 实用经验教程分享!

    这里没有做所有html标签的初始化,根据个人项目自己初始化标签默认样式。851办公区 - 实用经验教程分享!

    html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

  • 4

    书写并添加js代码。851办公区 - 实用经验教程分享!

    script>851办公区 - 实用经验教程分享!

    var canvas, ctx, width, height, stems, bubbles;851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    stems = [];851办公区 - 实用经验教程分享!

    bubbles = [];851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function Bubble(x, y, radius) {851办公区 - 实用经验教程分享!

    this.x = x;851办公区 - 实用经验教程分享!

    this.y = y;851办公区 - 实用经验教程分享!

    this.radius = radius;851办公区 - 实用经验教程分享!

    this.vy = -Math.random() * 5;851办公区 - 实用经验教程分享!

    this.opacity = 0.2 Math.random() * 0.5;851办公区 - 实用经验教程分享!

    this.oldY = y;851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    Bubble.prototype.draw = function() {851办公区 - 实用经验教程分享!

    var strokeColor, fillColor;851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    strokeColor = 'rgba(255, 255, 255,' this.opacity ')';851办公区 - 实用经验教程分享!

    fillColor = 'rgba(255, 255, 255,' (this.opacity / 2) ')';851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    ctx.save();851办公区 - 实用经验教程分享!

    ctx.lineWidth = 0.8;851办公区 - 实用经验教程分享!

    ctx.strokeStyle = strokeColor;851办公区 - 实用经验教程分享!

    ctx.fillStyle = fillColor;851办公区 - 实用经验教程分享!

    ctx.beginPath();851办公区 - 实用经验教程分享!

    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);851办公区 - 实用经验教程分享!

    ctx.closePath();851办公区 - 实用经验教程分享!

    ctx.fill();851办公区 - 实用经验教程分享!

    ctx.stroke();851办公区 - 实用经验教程分享!

    ctx.restore();851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function Stem(points, color) {851办公区 - 实用经验教程分享!

    this.points = points;851办公区 - 实用经验教程分享!

    this.color = color;851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    Stem.prototype.draw = function(ctx) {851办公区 - 实用经验教程分享!

    var len, ctrlPoint, point;851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    len = this.points.length - 1;851办公区 - 实用经验教程分享!

    ctrlPoint = {x: 0, y: 0};851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    ctx.save();851办公区 - 实用经验教程分享!

    ctx.strokeStyle = this.color;851办公区 - 实用经验教程分享!

    ctx.beginPath();851办公区 - 实用经验教程分享!

    ctx.moveTo(this.points[this.points.length - 1].x, this.points[this.points.length - 1].y);851办公区 - 实用经验教程分享!

    for (var i = len; i >= 1; i--) {851办公区 - 实用经验教程分享!

    point = this.points[i];851办公区 - 实用经验教程分享!

    ctrlPoint.x = (point.x this.points[i - 1].x) / 2;851办公区 - 实用经验教程分享!

    ctrlPoint.y = (point.y this.points[i - 1].y) / 2;851办公区 - 实用经验教程分享!

    ctx.quadraticCurveTo(point.x, point.y, ctrlPoint.x, ctrlPoint.y);851办公区 - 实用经验教程分享!

    ctx.lineWidth = i * 1.1;851办公区 - 实用经验教程分享!

    ctx.stroke();851办公区 - 实用经验教程分享!

    ctx.fillStyle = 'red';851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    ctx.restore();851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    init();851办公区 - 实用经验教程分享!

    function init(){851办公区 - 实用经验教程分享!

    canvas = document.querySelector('canvas');851办公区 - 实用经验教程分享!

    ctx = canvas.getContext('2d');851办公区 - 实用经验教程分享!

    width = canvas.width = window.innerWidth;851办公区 - 实用经验教程分享!

    height = canvas.height = window.innerHeight;851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    populateStems(height / 3, width, 25);851办公区 - 实用经验教程分享!

    generateBubbles(50);851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    drawFrame();851办公区 - 实用经验教程分享!

    };851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function generateBubbles(bubblesLimit) {851办公区 - 实用经验教程分享!

    for (var i = 0; i = bubblesLimit; i ) {851办公区 - 实用经验教程分享!

    bubbles.push(new Bubble(Math.random() * width, height Math.random() * height / 2, 2 Math.random() * 2));851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function populateStems(offset, limit, step) {851办公区 - 实用经验教程分享!

    for (var x = 0; x = limit; x = step) {851办公区 - 实用经验教程分享!

    generateStem(x, height / 2 - offset / 2 Math.random() * offset, 50)851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function generateStem(x, pointsLen, step) {851办公区 - 实用经验教程分享!

    var positions, y, offset, colorsArr, color;851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    colorsArr = ['#6e881b', '#5d7314', '#54690f', '#657f0f', '#6f8f06'];851办公区 - 实用经验教程分享!

    color = Math.floor(1 Math.random() * colorsArr.length - 1);851办公区 - 实用经验教程分享!

    positions = [];851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    if (height 600) {851办公区 - 实用经验教程分享!

    offset = -40 Math.random() * 80;851办公区 - 实用经验教程分享!

    for (y = height - pointsLen; y = height 100; y = step / 2) {851办公区 - 实用经验教程分享!

    positions.push({x: x offset / (y / 2000), y: y, angle: Math.random() * 360, speed: 0.1 Math.random() * 0.3});851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    } else {851办公区 - 实用经验教程分享!

    offset = -100 Math.random() * 200;851办公区 - 实用经验教程分享!

    for (y = height - pointsLen; y = height 100; y = step) {851办公区 - 实用经验教程分享!

    positions.push({x: x offset / (y / 2000), y: y, angle: Math.random() * 360, speed: 0.1 Math.random() * 0.3});851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    stems.push(new Stem(positions, colorsArr[color]));851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function drawFrame() {851办公区 - 实用经验教程分享!

    window.requestAnimationFrame(drawFrame, canvas);851办公区 - 实用经验教程分享!

    ctx.fillStyle = '#17293a';851办公区 - 实用经验教程分享!

    ctx.fillRect(0, 0, width, height);851办公区 - 实用经验教程分享!

    bubbles.forEach(moveBubble);851办公区 - 实用经验教程分享!

    stems.forEach(function(stem) {851办公区 - 实用经验教程分享!

    stem.points.forEach(movePoint);851办公区 - 实用经验教程分享!

    stem.draw(ctx);851办公区 - 实用经验教程分享!

    });851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function moveBubble(bubble) {851办公区 - 实用经验教程分享!

    if (bubble.y bubble.radius = 0) bubble.y = bubble.oldY;851办公区 - 实用经验教程分享!

    bubble.y = bubble.vy;851办公区 - 实用经验教程分享!

    bubble.draw(ctx);851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    851办公区 - 实用经验教程分享!

    function movePoint(point, index) {851办公区 - 实用经验教程分享!

    point.x = Math.sin(point.angle) / (index / 2.2);851办公区 - 实用经验教程分享!

    point.angle = point.speed;851办公区 - 实用经验教程分享!

    }851办公区 - 实用经验教程分享!

    /script>851办公区 - 实用经验教程分享!

    html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

  • 5

    代码整体结构。851办公区 - 实用经验教程分享!

    html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

  • 6

    查看效果。851办公区 - 实用经验教程分享!

    html5 canvas海底水草动画特效851办公区 - 实用经验教程分享!

  • 6本页面未经授权抓取自百度经验
  • 以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!851办公区 - 实用经验教程分享!


    标签: HTML

    办公区 Copyright © 2016-2023 www.bgqu.net. Some Rights Reserved. 备案号:湘ICP备2020019561号统计代码