Home > Web Front-end > H5 Tutorial > How canvas implements github404 dynamics

How canvas implements github404 dynamics

php中世界最好的语言
Release: 2018-01-30 09:21:34
Original
2278 people have browsed it

This time I will show you how to implement github404 dynamics with canvas. What are the precautions for using canvas to implement github404 dynamics? Here are practical cases, let’s take a look.

A few days ago, I used css style and js to pay tribute to the similar interface of github404. At the same time, I recently came into contact with canvas. With the idea of ​​messing around, I used the previous js algorithm to complete it using canvas. Dynamic effects of github404.

File resources

File source code and PicturesThe

code is given at the end of the article

The body part of the webpage

Here, define the width and height of canvas and set it as a block-level element. These img tags load these images, so we don’t need to load them in js, and then set the images not to be displayed display:none.

<body>
    <canvas id="mycanvas" width="1680" height="630"
        style="margin:0;display:block">
            您的浏览器不支持canvas
    </canvas>
    <img src="./images/field.png" style="display:none">
    <img src="./images/text.png" style="display:none">
    <img src="./images/cat.png" style="display:none">
    <img src="./images/cat_shadow.png" style="display:none">
    <img src="./images/speeder.png" style="display:none">                       
    <img src="./images/speeder_shadow.png" style="display:none">           
    <img src="./images/buliding_1.png" style="display:none">
    <img src="./images/building_2.png" style="display:none"> 
 </body>
Copy after login

js part

1. Here I still created a new json object named github404 to encapsulate all parameters and methods

2. Create an imgData object again, and pass all the parameters required for img into ps:top and left for positioning in the drawImage() method. The scale parameter is used to calculate the corresponding picture movement when the mouse moves

3. The init() method is used for initialization and is the interface with the outside

4. The implementation of the drawing method is to use the for in loop to traverse imgData[], then assign values ​​in sequence, and finally use the drawImage() method to draw, just In the mobile drawing method, you need to pay attention to using the ctx.clearRect() method to clear the canvas first.

<script>
        var github404 = {
            imgData: {//将所有图片的信息用json对象记录
                bg: {
                    top: 0,
                    left: 110,//top和left用于定位,在画图时使用
                    src: &#39;./images/field.png&#39;,//对应图片路径
                    scale: 0.06,//鼠标移动时,该图片所对应移动的比例
                },
                building_2: {
                    top: 133,
                    left: 1182,
                    src: &#39;./images/building_2.png&#39;,
                    scale: 0.05,
                },
                building_1: {
                    top: 79,
                    left: 884,
                    src: &#39;./images/buliding_1.png&#39;,
                    scale: 0.03,
                },
                speeder_shadow: {
                    top: 261,
                    left: 776,
                    src: &#39;./images/speeder_shadow.png&#39;,
                    scale: 0.01,
                },
                cat_shadow: {
                    top: 288,
                    left: 667,
                    src: &#39;./images/cat_shadow.png&#39;,
                    scale: 0.02,
                },
                speeder: {
                    top: 146,
                    left: 777,
                    src: &#39;./images/speeder.png&#39;,
                    scale: 0.01,
                },
                cat: {
                    top: 88,
                    left: 656,
                    src: &#39;./images/cat.png&#39;,
                    scale: 0.05,
                },
                text: {
                    top: 70,
                    left: 364,
                    src: &#39;./images/text.png&#39;,
                    scale: 0.03,
                },
            },
            rate_w: 0,
            rate_h: 0,//偏移的比例
            field_width: 1680,
            field_height: 370,//背景高度和宽度
            canvas: document.querySelector(&#39;#mycanvas&#39;),//获得canvas元素
 
            init: function() {//初始化加载方法
                this.setRateWH();
                this.placeImg();
                this.attachMouseEvent();
            },
            setRateWH: function() {//计算偏移比的方法
                var window_width = document.body.clientWidth;
                var window_height = document.body.clientHeight;
                this.rate_w = this.field_width/window_width;
                this.rate_h = this.field_height/window_height;
            },
 
            placeImg: function() {//初始化的绘图方法
                let ctx = this.canvas.getContext(&#39;2d&#39;);//获得画笔
                for(key in this.imgData){//遍历imageData 对象
                    var image = new Image();
                    var left = this.imgData[key].left;
                    var top = this.imgData[key].top;   
                    image.src = this.imgData[key].src;
                    ctx.drawImage(image,left,top,
                        image.width,image.height);
                }
 
            },
 
            attachMouseEvent: function() {
                var that = this;
                document.body.onmousemove = function(e){
                    that.picMove(e.pageX,e.pageY);
                }
            },
            picMove: function(pageX,pageY) {//鼠标移动时重新画图的方法
                let ctx = this.canvas.getContext(&#39;2d&#39;);
                ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
                for(key in this.imgData) {
                    var image = new Image();
                    var offer_w = this.rate_w * pageX * this.imgData[key].scale;
                var offer_h = this.rate_h * pageY * this.imgData[key].scale;
                    //定义 left和top,下面画图时给参数定位
                    var left = this.field_width/100 - offer_w + this.imgData[key].left;
                    var top = this.field_height/100 - offer_h + this.imgData[key].top;
                    image.src = this.imgData[key].src;
                    ctx.drawImage(image,left,top,
                        image.width,image.height);
                }
            }
        }
 
        window.onload = function() {
            //只调用github404的init方法 封装了数据
            github404.init();
        }
    </script>
Copy after login

Summary

This time I used canvas to complete this dynamic effect, which made me understand more about the usage of canvas. At the same time, it gave me a deeper understanding of how to use json objects to encapsulate data and methods, and how to organize code.

I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How the HTML5 drop-down box should increase the user experience


How to segment the H5 file domain FileReader Read the file and upload it to the server


How to use H5's WebGL to make json and echarts charts in the same interface

The above is the detailed content of How canvas implements github404 dynamics. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template