利用纯JS和HTML Canvas生成随机迷宫过程中产生的有趣的事情

上效果图:

#先看生成随机迷宫的代码吧↓

 1 <html>
 2 <head>
 3 <title>生成随机迷宫v1.0</title>
 4 </head>
 5 <body>
 6 <center style="margin-top: 20px;">
 7     <canvas id="myCan1" title="作者:谢辉"></canvas>
 8     <p>生成随机迷宫v1.0</p>
 9     <button onclick="history.go(0);">刷新</button>
10 </center>
11 </body>
12 <script>
13 var width = 1600;
14 var height = 800;
15 var cellWidth = 40;
16 var canvas = document.getElementById("myCan1");
17 var cxt = canvas.getContext("2d");
18 // 初始化canvas容器
19 function initCanvas() {
20     canvas.width = width;
21     canvas.height = height;
22     canvas.style = "border-radius: 15px";
23     canvas.style.border = "1px solid #3b3b3b";
24 };
25 initCanvas();
26 // 初始化画布
27 function initContext(cxt) {
28     cxt.lineCap="round";
29     cxt.lineJoin="round";
30     cxt.lineWidth = 1;
31 }
32 initContext(cxt);
33 // 开始画迷宫
34 function drawMaze(cxt) {
35     drawSingleBox(8, 0, 0, cxt);
36 };
37 /*setInterval(function(){
38     cxt.clearRect(0, 0, width, height);
39     drawMaze(cxt);
40 }, 50);*/
41 drawMaze(cxt);
42 // 递归画单元格
43 function drawSingleBox(option, posX, posY, cxt){
44     //cxt.strokeStyle = getColor();
45     switch(option){
46     case 0:
47         cxt.beginPath();
48         cxt.moveTo(posX, posY);
49         cxt.lineTo(posX + cellWidth, posY);
50         cxt.stroke();
51         break;
52     case 1:
53         cxt.beginPath();
54         cxt.moveTo(posX + cellWidth, posY);
55         cxt.lineTo(posX + cellWidth, posY + cellWidth);
56         cxt.stroke();
57         break;
58     case 2:
59         cxt.beginPath();
60         cxt.moveTo(posX, posY + cellWidth);
61         cxt.lineTo(posX + cellWidth, posY + cellWidth);
62         cxt.stroke();
63         break;
64     case 3:
65         cxt.beginPath();
66         cxt.moveTo(posX, posY);
67         cxt.lineTo(posX, posY + cellWidth);
68         cxt.stroke();
69         break;
70     }
71     if(posX >= width - cellWidth && posY >= height - cellWidth) {
72         return;
73     } else {
74         posX += cellWidth;
75         if(posX >= width){
76             posX = 0;
77             posY += cellWidth;
78         }
79         drawSingleBox(Math.floor(Math.random()*4), posX, posY, cxt);
80     }
81 }
82 // 随机出任意颜色
83 function getColor(){
84     var colorElements = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
85     var colorArray = colorElements.split(",");
86     var color ="#";
87     for(var i =0;i<6;i++){
88         color+=colorArray[Math.floor(Math.random()*16)];
89     }
90     return color;
91 }
92 </script>
93 </html>

生成随机迷宫

#去掉代码注释,神奇的一幕出现了,自己看看吧

1 setInterval(function(){
2     cxt.clearRect(0, 0, width, height);
3     drawMaze(cxt);
4 }, 50);

去掉此段代码的注释

原文地址:https://www.cnblogs.com/xhelper/p/9182275.html

时间: 2024-07-30 08:48:07

利用纯JS和HTML Canvas生成随机迷宫过程中产生的有趣的事情的相关文章

微信小程序 canvas 生成随机验证码

https://blog.csdn.net/qq_16646819/article/details/81020245?utm_source=blogxgwz0 js 1 // pages/bind/bind.js 2 var app = getApp(); 3 var baseUrl = getApp().baseUrl; 4 var ctx; 5 Page({ 6 7 /** 8 * 页面的初始数据 9 */ 10 data: { 11 text: '', 12 }, 13 14 /** 15

[PHP]利用openssl_random_pseudo_bytes和base64_encode函数来生成随机字符串

openssl_random_pseudo_bytes函数本身是用来生成指定个数的随机字节,因此在使用它来生成随机字符串时,还需要配合使用函数base64_encode.如下所示: public static function getRandomString($length = 42) { /* * Use OpenSSL (if available) */ if (function_exists('openssl_random_pseudo_bytes')) { $bytes = openss

用Canvas生成随机验证码(后端前端都可以)

一 .使用前端生成验证码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <canvas id="canvas" width="120" height="40"></canvas> <

纯js生成QRCode

纯js,不依赖jquery,非常好用,废话不多说,直接上代码! 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"

js生成随机固定长度字符串的简便方法

概述 碰到一个需求:用js生成固定长度的字符串.在网上查了很多资料,网上的方法都比较麻烦.我自己灵光一现,实现了一个比较简单的方法.记录下来,供以后开发时参考,相信对其他人也有用. js生成随机字符串 js生成随机字符串有一个奇妙的写法: //输出随机字符串 const randStr = () => Math.random().toString(36).substr(2); 浏览器开发者工具输入5次,输出如下: "4cc9gd4sbwd" "ox9r8g6g7h&qu

利用tween.js算法生成缓动效果

在讲tween类之前,不得不提的是贝塞尔曲线了.首先,贝塞尔曲线是指依据四个位置任意的点坐标绘制出的一条光滑曲线.它在作图工具或动画中中运用得比较多,例如PS中的钢笔工具,firework中的画笔等等.无论运用在哪里,它们的原理都是一样的.同样,在用js实现运动效果时,我们也可以利用贝塞尔曲线来实现不同的特效,而tween.js就是一个封装好的计算辅助算法.你可以通过连续输入多个值,然后利用贝塞尔曲线公式输出不同的值,最终形成了一条光滑的曲线.因为一条曲线上的值的不一样的,所以我们可以利用曲线的

利用Java随机,生成随机学生数据

为模拟向数据库中大量插入学生数据(注:此处应该用PreparedStatement.batchUpdate等批处理提高效率)的情形,通过Java随机来生成学生数据. 一.要生成的学生数据 students表设计如下: 其中前三项是数据库自动生成的,后面的10项需要程序生成. >>基于实际要求,插入的Student数据中,major和jnshuId不能同时相同.但由于随机数的不确定性,在程序中限制两个Student的major和jnshuId不同时相同复杂而低效.因此,选择在数据库中将major

ios系统 竖屏拍照 canvas处理后 图片旋转(利用exif.js解决ios手机上传竖拍照片旋转90度问题)

转:https://www.cnblogs.com/lovelgx/articles/8656615.html ---恢复内容开始--- 问题:html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 解决方法:利用exif.js解决ios手机上传竖拍照片旋转90度问题 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非横拍的ios照片进行角度旋转修正. 利用exif.js读取照片的拍摄信息,详见 htt

原生js实现一个连连看小游戏(二)-----------生成随机不重复数字

直接贴代码: <!DOCTYPE html> <html> <head> <title>生成随机不重复数</title> </head> <body> <script type="text/javascript"> var arr=new Array(); for(var i=0;i<10;i++){ arr.push(i); } // console.log(arr) var get