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="/static/imghwm/default1.png" data-src="./images/field.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/text.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/cat.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/cat_shadow.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/speeder.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/speeder_shadow.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/buliding_1.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > <img src="/static/imghwm/default1.png" data-src="./images/building_2.png" class="lazy" style="max-width:90%" alt="How canvas implements github404 dynamics" > </body>
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: './images/field.png',//对应图片路径 scale: 0.06,//鼠标移动时,该图片所对应移动的比例 }, building_2: { top: 133, left: 1182, src: './images/building_2.png', scale: 0.05, }, building_1: { top: 79, left: 884, src: './images/buliding_1.png', scale: 0.03, }, speeder_shadow: { top: 261, left: 776, src: './images/speeder_shadow.png', scale: 0.01, }, cat_shadow: { top: 288, left: 667, src: './images/cat_shadow.png', scale: 0.02, }, speeder: { top: 146, left: 777, src: './images/speeder.png', scale: 0.01, }, cat: { top: 88, left: 656, src: './images/cat.png', scale: 0.05, }, text: { top: 70, left: 364, src: './images/text.png', scale: 0.03, }, }, rate_w: 0, rate_h: 0,//偏移的比例 field_width: 1680, field_height: 370,//背景高度和宽度 canvas: document.querySelector('#mycanvas'),//获得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('2d');//获得画笔 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('2d'); 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>
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 experienceHow to segment the H5 file domain FileReader Read the file and upload it to the serverHow to use H5's WebGL to make json and echarts charts in the same interfaceThe above is the detailed content of How canvas implements github404 dynamics. For more information, please follow other related articles on the PHP Chinese website!

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)