popup(3)
来源:互联网 发布:淘宝手机店铺装修教程 编辑:程序博客网 时间:2024/06/05 16:35
在body中我只放了一个canvas元素定义宽高;
<canvas id="cloth" width="500" height="500"></canvas>
定义canvas的背景吧:
canvas { background: yellow; }
然后就是编写脚本了;
<script>//获取元素创建画布 var ctx = document.getElementById("cloth").getContext("2d"); //定义矩形类,默认四个属性 var Rect = function (x1, y1, x2, y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } //定义画矩形的方法 Rect.prototype.drawRect = function (x) { x.fillStyle = "#" + getRandomN(255, 0).toString(16) + getRandomN(255, 0).toString(16) + getRandomN(255, 0).toString(16); x.fillRect(this.x1, this.y1, this.x2, this.y2) }; //定义清除画布的方法 var clearRec = function () { ctx.clearRect(getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0)) } //定义获取随机数的方法 function getRandomN(max, min) { return Math.floor(Math.random() * (max - min) + min) } //定义创建一个矩形的函数 function createoneR() { var rec = new Rect(getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0)); rec.drawRect(ctx); } //定时器, setInterval(function () { createoneR(); createoneR(); clearRec(); createoneR(); clearRec(); }, 200)</script>
生成的效果是
源码是:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> canvas { background: yellow; } </style></head><body><canvas id="cloth" width="500" height="500"></canvas><script> var ctx = document.getElementById("cloth").getContext("2d"); var Rect = function (x1, y1, x2, y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } Rect.prototype.drawRect = function (x) { x.fillStyle = "#" + getRandomN(255, 0).toString(16) + getRandomN(255, 0).toString(16) + getRandomN(255, 0).toString(16); x.fillRect(this.x1, this.y1, this.x2, this.y2) }; var clearRec = function () { ctx.clearRect(getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0)) } function getRandomN(max, min) { return Math.floor(Math.random() * (max - min) + min) } var rect1 = new Rect(10, 10, 100, 100); function createoneR() { var rec = new Rect(getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0), getRandomN(500, 0)); rec.drawRect(ctx); } setInterval(function () { createoneR(); createoneR(); clearRec(); createoneR(); clearRec(); }, 200)</script></body></html>
阅读全文
0 1
- popup(3)
- POPUP
- Popup
- popup
- popup
- popup
- popup
- popup
- popup
- popup
- popup?
- popup
- popup
- OpenLayers 3 之 弹出框(popup)
- Swift 3 popup model dialog传递数据
- 使用Popup窗口创建无限级Web页菜单(3)
- 使用Popup窗口创建无限级Web页菜单(3)
- UNITY 3D 下拉列表的设置(popup list)
- dialog.JS 异步方式处理dialog封装
- Harmonic Number 调和级数的欧拉公式
- 配置tomcat
- python操作数据库(pymysql 和 SQLAchemy)
- Greenplum介绍
- popup(3)
- I2C总线 驱动程序设计 --- EEPROM 驱动设计
- DirectX笔记 数学基础
- Windows 安装 MongoDB
- 百度云盘分享视频教程:java经典面试题深度解析
- 1977-汉诺塔问题
- C++中默认定义全局变量
- 技术点-Maven-多源文件,多资源文件配置
- 点击空白处,软键盘消失