HTML5 Canvas绘文本动画(使用CSS自定义字体)

一、HTML代码:

<!DOCTYPE html>
 <html>
  <head>
   <title>Matrix Text - HTML5 Canvas Demo</title>
   <script type="text/javascript" src="matrixtext.js"></script>
  <style>
   @font-face
   {
     font-family: ‘MatrixCode‘;
     src: url("matrix code nfi.otf") format("opentype");
    }
  html, body
  {
   background: #000;
   margin: 0;
   padding: 0;
   overflow-x: hidden;
   overflow-y: hidden;
  }
   canvas { font-family: ‘MatrixCode‘; }
   p
  {
    font-family: ‘MatrixCode‘;
    color: #fff;
   }
  </style>
</head>
<body onload="init();">
  <canvas id="theMatrix" width="1280" height="800"> <p>Your browser doesn‘t support canvas...L0Z3R</p>
  </canvas>
</body>
</html>

二、JS代码:

var textStrip = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘,
                  ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘];
 var stripCount=90, stripX=new Array(), stripY=new Array(), dY=new Array(), stripFontSize = new Array();
 for (var i = 0; i < stripCount; i++) {
    stripX[i] = Math.floor(Math.random()*1265);
    stripY[i] = -100;
    dY[i] = Math.floor(Math.random()*7)+3;
    stripFontSize[i] = Math.floor(Math.random()*24)+12;
 }
 var theColors = [‘#cefbe4‘, ‘#81ec72‘, ‘#5cd646‘, ‘#54d13c‘, ‘#4ccc32‘, ‘#43c728‘];
 var elem, context, timer;
 function drawStrip(x, y) {
    for (var k = 0; k <= 20; k++) {
        var randChar = textStrip[Math.floor(Math.random()*textStrip.length)];
        if (context.fillText) {
            switch (k) {
            case 0:
                context.fillStyle = theColors[0]; break;
            case 1:
                context.fillStyle = theColors[1]; break;
            case 3:
                context.fillStyle = theColors[2]; break;
            case 7:
                context.fillStyle = theColors[3]; break;
            case 13:
                context.fillStyle = theColors[4]; break;
            case 17:
                context.fillStyle = theColors[5]; break;
            }
            context.fillText(randChar, x, y);
        }
        y -= stripFontSize[k];
    }
  }
  function draw() {
    // clear the canvas and set the properties
    context.clearRect(0, 0, elem.width, elem.height);
    context.shadowOffsetX = context.shadowOffsetY = 0;
    context.shadowBlur = 8;
    context.shadowColor = ‘#94f475‘;

    for (var j = 0; j < stripCount; j++) {
        context.font = stripFontSize[j]+‘px MatrixCode‘;
        context.textBaseline = ‘top‘;
        context.textAlign = ‘center‘;

        if (stripY[j] > 1358) {
            stripX[j] = Math.floor(Math.random()*elem.width);
            stripY[j] = -100;
            dY[j] = Math.floor(Math.random()*7)+3;
            stripFontSize[j] = Math.floor(Math.random()*24)+12;
            drawStrip(stripX[j], stripY[j]);
        } else drawStrip(stripX[j], stripY[j]);

        stripY[j] += dY[j];
    }
  }
  function init() {
    // get the canvas‘ id
    elem = document.getElementById(‘theMatrix‘);
    if (!elem || !elem.getContext) return;

    // get the canvas‘ context
    context = elem.getContext(‘2d‘);
    if (!context) return;

    timer = setInterval(‘draw()‘, 70);
  }
时间: 2024-08-10 06:43:53

HTML5 Canvas绘文本动画(使用CSS自定义字体)的相关文章

HTML5 Canvas水波纹动画特效

HTML5的Canvas特性非常实用,我们不仅可以在Canvas画布上绘制各种图形,也可以制作绚丽的动画,比如这次介绍的水波纹动画特效.以前我们也分享过一款基于HTML5 WebGL的水波荡漾动画,让人惊叹不已,这次分享的HTML5 Canvas水波纹动画同样非常震撼人心. 在线演示          源码下载 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&q

HTML5 Canvas火焰闪烁动画 火焰跟随鼠标

HTML5 Canvas火焰闪烁动画 火焰跟随鼠标 该款动画是基于Canvas,火焰上下窜动的效果非常逼真,而且火焰可以跟随鼠标移动,是一款非常酷炫的HTML5 Canvas动画. 下载地址:http://www.devstore.cn/code/info/262.html 运行截图: 版权声明:本文为博主原创文章,未经博主允许不得转载.

【转】HTML5 Canvas头发飘逸动画

原文:HTML5 Canvas头发飘逸动画 HTML5 Canvas动画非常炫酷,很多有创意的开发者可以利用HTML5的Canvas特性创造出很多不错的动画特效.今天我们要分享一款HTML5 Canvas头发飘逸动画,该动画模拟头发飘动的效果,加上可爱的人脸,可以说非常有技术含量,更富有创意. 点击图片演示: 源码下载

css自定义字体

@font-face { font-family: 华文隶书; src: url( ../font/STLITI.eot ); /* IE */ src: url( ../font/STLITI.ttf ) format("truetype"); /* 非IE */ } #nav{width:30px;height:537px;display:block;font-size:18px;text-align:center;margin-left:10px;font-family:&quo

HTML5 Canvas核心技术图形动画与游戏开发(读书笔记)----第一章,基础知识

一,canvas元素 1 为了防止浏览器不支持canvas元素,我们设置“后备内容”(fallback content),下面紫色的字即为后备内容 <canvas id="canvas" width="600" height="300">您的浏览器不支持canvas,可以选择升级您的浏览器</canvas> 2 开发基于canvas的应用程序的最基本的几个操作 1),使用document.getElementById()方

HTML5 canvas绘制文本

demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>绘制文字</title> <link rel="stylesheet" type="text/css" href="style.css"> <script ty

学习HTML5, Canvas及简单动画的使用

通过在CSDN中的一些简单课程学习,跟随老师联系,做了如下的月亮,太阳和地球的运动效果.纪录在文章中,用于下次使用. 准备工作如下: 1. 使用三张背景图片 太阳 月亮 地球 2. 在HTML页面中定义一个CANVAS 1 <body> 2 <div style="background-color:green;text-align:center ;"> 3 <canvas id="c" style="background-co

[转] css自定义字体font-face的兼容和使用

@Font-face目前浏览器的兼容性: Webkit/Safari(3.2+) TrueType/OpenType TT (.ttf) .OpenType PS (.otf): Opera (10+) TrueType/OpenType TT (.ttf) . OpenType PS (.otf) . SVG (.svg): Internet Explorer 自ie4开始,支持EOT格式的字体文件:ie9支持WOFF: Firefox(3.5+) TrueType/OpenType TT (

css自定义字体完美解决方案example

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>fontface</title> 6 <style type="text/css"> 7 8 /*完美解决css3 web-safe字体各种浏览器兼容问题*/ 9 @font-face { 10 font-