Canvas实例

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        body { background: #141414; overflow: hidden; }
    </style>
</head>
<body>
    <canvas id="c"></canvas>
    <script>
        window.onload = function (argument) {
            var lyric = "i couldn‘t take it couldn‘t stand another minute couldn‘t bear another day without you in it all of the joy that I had known for my life was stripped away from me the minute that you died to have you in my life was all i ever wanted but now without you I‘m a soul forever haunted can‘t help but feel that i had taken you for granted no way in hell that i can ever comprehend this i wasn‘t dreaming when they told me you were gone i was wide awake and feeling that they had to be wrong how could you leave me when you swore that you would stay now i‘m trapped inside a nightmare every single fucking day it‘s like a movie but there‘s not a happy ending every scene fades black and there‘s no pretending this little fairy tale doesn‘t seem to end well theres no knight in shining armor who will wake me from the spell i know you didn‘t plan this you tried to do what‘s right but in the middle of this madness i‘m the one you left to win this fight red like roses fills my head with dreams and finds me always closer to the emptiness and sadness that has come to take the place of you i know you‘re broken down by anger and by sadness you feel I left you in a world that‘s full of madness wish i could talk to you if only for a minute make you understand the reasons why i did it i wanna tell you that you‘re all that ever mattered want you to know that for eternity i‘m shattered i tried so hard just to protect you but i failed to and in a prison of abandonment i‘ve jailed you i never planned that i would leave you there alone i was sure that i would see you when i made it back home and all the times I swore that it would be okay now i‘m nothing but a liar and you‘re thrown into the fray this bedtime story ends with misery ever after the pages are torn and there‘s no final chapter i didn‘t have a choice I did what I had to do i made a sacrifice but forced a bigger sacrifice on you i know you‘ve lived a nightmare i caused you so much pain but baby please don‘t do what i did i don‘t want you to waste your life in vain red like roses fills my head with dreams and finds me always closer to the emptiness and sadness that has come to take the place of you you‘re not the only one who needed me i thought you understood you were the one i needed and you left me as I always feared you would would I change it if i could? it doesn‘t matter how the petals scatter now every nightmare just discloses it‘s your blood that‘s red like roses and no matter what I do nothing ever takes the place of you red like roses fills my head with dreams and finds me always closer to the emptiness and sadness that has come to take the place of you";
            var words = {};
            var words_attr = [];
            string_handle(lyric);

            var canvas = document.getElementById(‘c‘);
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;

            if (canvas.getContext) {
                var c = canvas.getContext(‘2d‘),
                    w = canvas.width,
                    h = canvas.height;

                c.strokeStyle = ‘red‘;
                c.fillStyle = ‘white‘;
                c.lineWidth = 5;

                // constructor
                Word = function (key) {
                    this.text = key;
                    this.x = Math.random() * w;
                    this.y = Math.random() * h;
                    this.font = words[key] * 10 + ‘px arial‘
                    this.speed = (words[key]);
                }
                for (key in words) {
                    words_attr.push(new Word(key));
                }
                console.log(words_attr.length);

                function animation() {
                    for (var i = 0; i < words_attr.length; i++) {
                        c.font = words_attr[i].font;
                        c.fillText(words_attr[i].text, words_attr[i].x, words_attr[i].y);
                        words_attr[i].width = c.measureText(words_attr[i].text).width;
                        c.stroke();
                    }
                    move();
                }

                function move() {
                    for (var i = 0; i < words_attr.length; i++) {
                        if (words_attr[i].x > w) {
                            words_attr[i].x = -words_attr[i].width;
                            words_attr[i].y = Math.random() * h;
                        } else {
                            words_attr[i].x += words_attr[i].speed;
                        }
                    }
                }

                setInterval(function () {
                    c.clearRect(0, 0, w, h);
                    animation();
                }, 24);

            }

            function string_handle(str) {
                var split_str = str.split(" ");
                var word_array = [];
                var word_count = [];
                for (var i = 0; i < split_str.length; i++) {
                    check = true;
                    for (var j = 0; j <= word_array.length; j++) {
                        if (split_str[i] == word_array[j]) {
                            word_count[j]++;
                            check = false;
                            break;
                        }
                    }
                    if (check) {
                        word_array.push(split_str[i]);
                        word_count.push(1);
                    }
                }
                for (var i = 0; i < word_array.length; i++) {
                    words[word_array[i]] = word_count[i];
                }
                return words;
            }
        }
    </script>
</body>
</html>

来源:http://codepen.io/FlyC/pen/yaNVEQ

时间: 2024-10-06 02:47:39

Canvas实例的相关文章

超多经典 canvas 实例,动态离子背景、移动炫彩小球、贪吃蛇、坦克大战、是男人就下100层、心形文字等等等

超多经典 canvas 实例 普及:<canvas> 元素用于在网页上绘制图形.这是一个图形容器,您可以控制其每一像素,必须使用脚本来绘制图形. 注意:IE 8 以及更早的版本不支持 <canvas> 元素. 贴士:全部例子都分享在我的 GayHub - https://github.com/bxm0927/canvas-special 尤雨溪个人主页炫彩三角纽带效果,点击还可变换 GitHub源码 . Demo演示 知乎登录注册页动态离子背景效果 GitHub源码 . Demo演

canvas实例(基础)

JS实现五子棋大战:GitHub源码 知识点总结: 第一步.基础: 1 //获取canvas 2 var chess = document.getElementById('chess'); 3 4 //获取上下文,创建上下文对象 5 var context = chess.getContext('2d'); canvas绘制直线.设置画笔颜色 1 //设置画笔颜色 2 context.strokeStyle = '#bfbfbf'; 3 4 context.moveTo(0, 0); 5 con

canvas实例:旋转缩放的方块

首先在页面中创建一个canvas标签: <body> <canvas id="c1" width="500" height="500"></canvas> </body> js部分: 注意save()和restore()的运用,在num累加的情况下,保存路径和恢复路径可以让方块匀速运动: 定义变量num和value来设置临界点的方法. <script> window.onload = f

H5 Canvas

HTML 5 Canvas HTML5 拖放 HTML5 SVG canvas 元素用于在网页上绘制图形. 什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. 创建 Canvas 元素 向 HTML5 页面添加 canvas 元素. 规定元素的 id.宽度和高度: <canvas id="myCanvas" wid

Web 版 PowerDesigner (Canvas) 技术

什么是 Canvas?    HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. 以下是利用canvas写的web版PowerDesigner效果图: 在线演示:http://www.netuml.com:8088 创建 Canvas 元素 向 HTML5 页面添加 canvas 元素. 规定元素的 id.宽度和高度: <canvas id="my

Canvas 最佳实践(性能篇)

Canvas 想必前端同学们都不陌生,它是 HTML5 新增的「画布」元素,允许我们使用 JavaScript 来绘制图形.目前,所有的主流浏览器都支持 Canvas. Canvas 最常见的用途是渲染动画.渲染动画的基本原理,无非是反复地擦除和重绘.为了动画的流畅,留给我渲染一帧的时间,只有短短的 16ms.在这 16ms 中,我不仅需要处理一些游戏逻辑,计算每个对象的位置.状态,还需要把它们都画出来.如果消耗的时间稍稍多了一些,用户就会感受到「卡顿」.所以,在编写动画(和游戏)的时候,我无时

HTML5图形绘制学习(1)-- Canvas 元素简介

Canvas元素是HTML5中新增的一个专门用来进行图形绘制的元素.和其名称Canvas一样,它就相当于一个画布,我们可以在其上描绘各种图形. 这里所说的绘制图型,不是指我们可以进行可视化的图形绘制,而是要利用javascript在其中进行进行操作. 这里做个简单的canvas实例: 1.在页面中添加canvas元素 首先在页面中添加canvas元素,需要给定其高度.宽度和唯一标识id<!doctype html> <html lang="en"> <he

Canvas -- 角度与弧度

角度 直观说:角度也就是数学中的多少度.如 60°,180°,90° 其他表示方法: 90° 即 π / 2. 60° 即 π / 3. 180° 即 π. Canvas 中的弧度. 同角度一样.计算公式:假设当前弧度为 deg,( js 中的 π 用 Math.PI 表示) ** 弧度 = deg * Math.PI / 180 ** 以下canvas实例:通过角度去计算圆中某一点: 直接求得 x轴长度 , y轴长度需要使用 Math实例方法:Math.sin(),Math.cos(); le

canvas 元素用于在网页上绘制图形。

什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. 创建 Canvas 元素 向 HTML5 页面添加 canvas 元素. 规定元素的 id.宽度和高度: <canvas id="myCanvas" width="200" height="100"></canv