I saw Che Dabang’s article about realizing the Matrix rectangular array in the blog park. I felt that there are still some wonderful places in canvas, so I made a note to record it.
The effect achieved is as follows:
Really, adding one or two key lines of code can achieve unexpected effects.
Since it is implemented by canvas, the first step is to add the canvas tag to the page, as follows:
<canvas id="canvas">请使用高版本浏览器,IE8以及一下不支持canvas</canvas>
css code:
html,body{height:100%;overflow:hidden}
Since the effect is that the canvas is full screen Displayed in the browser, all the next step is to assign the width and height of the screen to canvas, as follows:
var width,height, canvas = document.getElementById("canvas"); canvas.width = width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; canvas.height = height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
Create a 2Ddrawing environment for canvas, as follows:
var ctx = canvas.getContext('2d');
Create an array below. Why use an array? It is mainly to use the array to store the Y value of the Chinese text in the canvas. If you don’t understand, you’ll understand after reading it.
var num = Math.ceil(width / 10); var y = Array(num).join(0).split('');
The value of num is to divide the width of the screen into num parts, that is, num columns. The width of each column is 10px.
Canvas drawing code:
var draw = function() { ctx.fillStyle = 'rgba(0,0,0,.05)'; //核心代码,创建黑色背景,透明度为0.05的填充色。 ctx.fillRect(0, 0, width, height); ctx.fillStyle = '#0f0'; //设置了字体颜色为绿色 ctx.font = '10px Microsoft YaHei';//设置字体大小与family for(i = 0; i < num; i++) { var x = (i * 10) + 10; text = String.fromCharCode(65 + Math.random() * 62); var y1 = y[i]; ctx.fillText(text, x, y1); if(y1 > Math.random() * 10 * height) { y[i] = 0; } else { y[i] = parseInt(y[i]) + 10; } } }
ctx.fillStyle = 'rgba(0,0,0,.05)' in the code. Since the page is repeatedly calling this draw method, the transparency is also When overlaying, the color of the text inside also changes, making everything look 3D and layered.
The main function of the for loop in the code is to set the text. The String.fromCharCode() method is used. The Unicode value is passed and the string is returned. For more information, you can view JavaScript Summary of series of methods and examples (1)
Then set the font position, for example, the font position of the first line is [10,10], [20,10], [30,10].... .;The font positions of the second line are [20,20],[30,20],[40,20]...and are derived in sequence.
So in the code var y = Array(num).join(0).split(''); is to save the y value of the font position, and then +10 in sequence through the for loop. Now I estimate that I know the array The function is to update the location.
The last step is to call this method repeatedly:
;(function(){ setInterval(draw, 100); })();
The map method used to implement the draw method online is actually the same. Paste the code:
$(document).ready(function() { var s = window.screen; var width = q.width = s.width; var height = q.height; var yPositions = Array(300).join(0).split(''); var ctx = q.getContext('2d'); var draw = function() { ctx.fillStyle = 'rgba(0,0,0,.05)'; ctx.fillRect(0, 0, width, height); ctx.fillStyle = 'red'; ctx.font = '10pt Georgia'; yPositions.map(function(y, index) { text = String.fromCharCode(1e2 + Math.random() * 33); x = (index * 10) + 10; q.getContext('2d').fillText(text, x, y); if(y > Math.random() * 1e4) { yPositions[index] = 0; } else { yPositions[index] = y + 10; } }); }; RunMatrix(); function RunMatrix() { Game_Interval = setInterval(draw, 1000); } });
Finally, the overall code:
<canvas id="canvas">请使用高版本浏览器,IE8以及一下不支持canvas</canvas>
[Related recommendations]
1. Special recommendation:"php Programmer Toolbox" V0.1 version download
2. Free h5 online video tutorial
3. php.cn original html5 video tutorial
The above is the detailed content of h5canvas implements the Matrix rectangular array effect code. For more information, please follow other related articles on the PHP Chinese website!

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

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.


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

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software