随机色块

代码描述:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>canvas运动</title>
<style>

</style>
</head>
<body>
<canvas width="500" height="300" style="border:1px solid black" id="canvas"></canvas>

<script>
var canvas = document.getElementById(‘canvas‘);
var ctx = canvas.getContext("2d");

var father = {};
father.children = [];
/*
* 增加孩子
*/
father.add = function (child) {
father.children.push(child);
}

/*
* 矩形类
*/
function Rect (x, y, w, h, style) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.style = style;
father.add(this);
this.render = function () {

}
}

// 生成一个随机位置、随机方向、随机速度、
// 随机颜色的矩形
function random () {
var x = Math.random() * 400;
var y = Math.random() * 200;
var w = Math.random() * 40 + 20;
var h = Math.random() * 40 + 20;
var colorr = Math.ceil(Math.random() * 256) - 1;//Math.ceil向正取整数
var colorg = Math.ceil(Math.random() * 256) - 1;
var colorb = Math.ceil(Math.random() * 256) - 1;
var color = "rgba("+colorr+","+colorg+","+colorb+",1)";
var t = new Rect(x,y,w,h,color);
t.dx = Math.ceil(Math.random()*2) - 1;
t.dy = Math.ceil(Math.random()*2) - 1;
t.speed = Math.ceil(Math.random()*3);
t.render = function () {
// left
if (this.dx == 0) {
this.x -= this.speed ;
if (this.x < 0) {
this.dx = 1;
this.x = 0;
};

// right
}else{
this.x += this.speed;
if (this.x > canvas.width - this.w) {
this.dx = 0;
this.x = canvas.width - this.w;
};
}

// 上
if (this.dy == 0) {
this.y -= this.speed ;
if (this.y < 0) {
this.dy = 1;
this.y = 0;
};

// 下
}else{
this.y += this.speed;
if (this.y > canvas.height - this.h) {
this.dy = 0;
this.y = canvas.height - this.h;
};
}

ctx.fillStyle = this.style;
ctx.fillRect(this.x,this.y,this.w,this.h);
}
}

// 生成10个随机矩形
for (var i = 0; i < 20; i++) {
random();
};
// 主时间轴计时器,控制所有对象的绘制
setInterval(function () {
ctx.clearRect(0,0,canvas.width,canvas.height);

for (var i = 0; i < father.children.length; i++) {

ctx.save();
father.children[i].render();
ctx.restore();
};
},10);

</script>
</body>
</html>

时间: 2024-08-01 10:45:37

随机色块的相关文章

颜色随机的小方块

用碎片加载小方块实现简单的随机色块,直接上代码,如果你嫌麻烦,可以看demo:http://codepen.io/koringz/pen/QbZEwx /**/js注释已删 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf

Swift语言iOS开发:CALayer十则示例(转)

http://mobile.51cto.com/iphone-469498.htm 如你所知,我们在iOS应用中看到的都是视图(view),包括按钮视图.表视图.滑动条视图,还有可以容纳其他视图的父视图等. 但你或许不知道在iOS中支撑起每个视图的是一个叫做"图层(layer)"的类,确切地说是CALayer. 本文中您会了解CALayer及其工作原理,还有应用CALayer打造酷炫效果的十则示例,比如绘制矢量图形.渐变色,甚至是粒子系统. 本文要求读者熟悉iOS应用开发和Swift语

IOS开发—找色块游戏

@interface NextViewController () { int r ; int m; int nn; UIView *view; NSArray *color; NSMutableArray *arry; UIImageView *baview; UILabel *textfiled; } @end @implementation NextViewController - (void)viewDidLoad { //    赋初值 建立最开始的模型 [super viewDidLo

JS实现找色块小游戏

之前用WPF的方式做了个简单的小游戏,找不同色块,WPF可以实现,JS更加Easy,于是又用JS做了一个,逻辑差不多,只是代码的实现方式有所不同 先上效果 关键代码如下 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; chars

(漂浮?气泡?)js生成位置、颜色、透明度随机的字块

效果图如下: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatibl

java中Random随机种子使用

在java中,通过Random生成随机数时,如果设置随机种子,则相同的种子,产生的随机数相同.若不设置则每次随机的不同. Random rnd = new Random(); rnd.setSeed(10);//用于设置种子. rnd.nextInt();// 用于产生随机数. rnd.nextInt(10); // 产生(0-9)数字.

【统计学习】随机梯度下降法求解感知机模型

1. 感知机学习模型 感知机是一个二分类的线性分类问题,求解是使误分类点到超平面距离总和的损失函数最小化问题.采用的是随机梯度下降法,首先任意选取一个超平面w0和b0,然后用梯度下降法不断地极小化目标损失函数,极小化过程中不是一次使所有误分类点的梯度下降,而是一次随机选取一个误分类点使其梯度下降.假设误分类点集合M是固定的,那么损失函数L(w,b)的梯度: 随机选取一个误分类点,对w和b进行更新: 其中n是步长,又称为学习率(learning rate),这样通过迭代可以使损失函数L(w,b)不

决策树 随机森林 adaboost

? 熵.互信息? 决策树学习算法 ? 信息增益 ? ID3.C4.5.CART? Bagging与随机森林? 提升 ? Adaboost/GDBT ? 熵.互信息 熵是对平均不确定性的度量. 平均互信息:得知特征Y的信息而使得对标签X的信息的不确定性减少的程度.描述随机变量之间的相似程度.(条件熵.相对熵:差异性) ? 决策树 决策树学习采用的是自顶向下的递归方法,有监督学习. 其基本思想是以信息熵为度量构造一棵熵值下降最快的树,到叶子节点处的熵值为零,此时每个叶节点中的实例都属于同一类. 建立

js里面随机抽取n个随机数

function getImageRandomPosition(){ do { var n = Math.floor(Math.random() * 12);//n为随机出现的0-11之内的数值 for (var i = 0; i < posArray.length; i++) { if (n == posArray[i]) { /*若n和数组里面的数值有重复,立即跳出函数*/ break; } } /*若n和数组里的数组无重复,那么i和数组的长度是相同的,这样可以避免出现重复的数字*/ if