js實(shí)現(xiàn)隨機(jī)圓與矩形功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)隨機(jī)圓與矩形功能的具體代碼,供大家參考,具體內(nèi)容如下
1、節(jié)點(diǎn)操作版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM節(jié)點(diǎn)操作版本</title>
</head>
<style>
*{
margin: 0;padding: 0;
}
html,body,.box{
width: 100%;height: 100%;
}
#content{
background: #f5f5f5;
}
.circle{
border-radius: 50%;position: absolute;
}
.rect{
position: absolute;
}
.btn{
position: fixed;top: 0;left: 0;
}
</style>
<body>
<div id="content" class="box"></div>
<div class="btn">
<button id="createCircle">創(chuàng)建隨機(jī)圓</button>
<button id="createRect">創(chuàng)建隨機(jī)矩形</button>
</div>
</body>
<script>
class Public{
constructor(){
this.x = this.randomR(0,1800);
this.y = this.randomR(40,806);
this.color = this.randomColor();
this.r = this.randomR(10,20);
}
randomR(min,max){//指定范圍內(nèi)的隨機(jī)半徑
return parseInt(Math.random() * (max - min) + min);
}
randomColor(){//隨機(jī)顏色
let rgba = `rgba(${this.randomR(0,255)},${this.randomR(0,255)},${this.randomR(0,255)},1)`;
return rgba;
}
}
class Circle extends Public{
constructor(){
super();
}
circle(){
const {r,x,y,color} = this;
const contentElem = document.getElementById("content");
let declareElem = document.createElement("div");
declareElem.className = "circle";
declareElem.style.width = `${r * 2}px`;
declareElem.style.height = `${r * 2}px`;
declareElem.style.background = color;
declareElem.style.top = `${y}px`;
declareElem.style.left = `${x}px`;
contentElem.appendChild(declareElem);
}
}
class Rect extends Public{
constructor(){
super();
}
rect(){
const {x,y,color} = this;
const contentElem = document.getElementById("content");
let declareElem = document.createElement("div");
declareElem.className = "rect";
declareElem.style.width = `${this.randomR(20,30)}px`;
declareElem.style.height = `${this.randomR(20,30)}px`;
declareElem.style.background = color;
declareElem.style.top = `${y}px`;
declareElem.style.left = `${x}px`;
contentElem.appendChild(declareElem);
}
}
document.getElementById("createCircle").onclick = () => {
new Circle().circle();
}
document.getElementById("createRect").onclick = () => {
new Rect().rect();
}
</script>
</html>
2、鼠標(biāo)拖動(dòng)版本(矩形版本類似)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>鼠標(biāo)拖動(dòng)版本</title>
</head>
<style>
*{
margin: 0;padding: 0;
}
html,body,.box{
width: 100%;height: 100%;
}
#content{
background: #f5f5f5;
}
.circle{
border-radius: 50%;position: absolute;
}
</style>
<body>
<div id="content" class="box"></div>
</body>
<script>
class Public{
constructor(x,y){
this.x = x;
this.y = y;
this.color = this.randomColor();
this.r = this.randomR(10,20);
}
randomR(min,max){//指定范圍內(nèi)的隨機(jī)半徑
return parseInt(Math.random() * (max - min) + min);
}
randomColor(){//隨機(jī)顏色
let rgba = `rgba(${this.randomR(0,255)},${this.randomR(0,255)},${this.randomR(0,255)},1)`;
return rgba;
}
}
class Circle extends Public{
constructor(x,y){
super(x,y);
}
circle(){
const {r,x,y,color} = this;
const contentElem = document.getElementById("content");
let declareElem = document.createElement("div");
declareElem.className = "circle";
declareElem.style.width = `${r * 2}px`;
declareElem.style.height = `${r * 2}px`;
declareElem.style.background = color;
declareElem.style.top = `${y}px`;
declareElem.style.left = `${x}px`;
contentElem.appendChild(declareElem);
setTimeout(() => {
declareElem.remove();
},100);
}
}
document.getElementById("content").onmousemove = (e) => {
const {clientX,clientY} = e || window.event;
new Circle(clientX,clientY).circle();
}
</script>
</html>
3、canvas版本
<!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-Compatible" content="ie=edge">
<title>Document</title>
<style>
#canvas{margin: 0 auto;background: #000;box-shadow: 0 0 10px #000;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<script>
var _={
random:function(start,stop){
return parseInt(Math.random()*(stop-start)+start);
}
}
window.onload=function(){
const canvas=document.getElementById("canvas");
const ctx=canvas.getContext("2d");
canvas.width="1000";
canvas.height="600";
class ball{
constructor(x,y,color){
this.x=x;
this.y=y;
this.r=40;
this.color=color;
}
render(){
ctx.save();
ctx.beginPath();
ctx.arc(this.x,this.y,this.r,0,Math.PI*2);
ctx.fillStyle=this.color;
ctx.fill();
ctx.restore();
}
}
class moveBall extends ball{
constructor(x,y,color){
super(x,y,color);
this.dx=_.random(-5,5);
this.dy=_.random(-5,5);
this.dr=_.random(1,3);
}
updated(){
this.x+=this.dx;
this.y+=this.dy;
this.r-=this.dr;
if(this.r<=0){
this.r=0;
}
}
}
let BallArr=[];
let Color=["red","green","blue","white","orange"];
canvas.addEventListener("mousemove",function(e){
let Ball=new moveBall(e.offsetX,e.offsetY,Color[_.random(0,Color.length-1)]);
BallArr.push(Ball);
});
setInterval(()=>{
ctx.clearRect(0,0,canvas.width,canvas.height);
for(let i=0;i<BallArr.length;i++){
BallArr[i].render();
BallArr[i].updated();
}
},50);
}
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用JSX實(shí)現(xiàn)Carousel輪播組件的方法(前端組件化)
做這個(gè)輪播圖的組件,我們先從一個(gè)最簡(jiǎn)單的 DOM 操作入手。使用 DOM 操作把整個(gè)輪播圖的功能先實(shí)現(xiàn)出來(lái),然后在一步一步去考慮怎么把它設(shè)計(jì)成一個(gè)組件系統(tǒng)2021-04-04
JavaScript中的宏任務(wù)和微任務(wù)執(zhí)行順序
在?JavaScript?中,宏任務(wù)和微任務(wù)是指在執(zhí)行代碼的過(guò)程中的兩種不同的任務(wù)類型,這篇文章主要介紹了JavaScript中的宏任務(wù)和微任務(wù)執(zhí)行順序,需要的朋友可以參考下2022-12-12
innerHTML在Mozilla Firefox和Opera下執(zhí)行的一個(gè)特例情況。
innerHTML在Mozilla Firefox和Opera下執(zhí)行的一個(gè)特例情況。...2007-01-01
JavaScript類型檢測(cè)之typeof 和 instanceof 的缺陷與優(yōu)化
在javascript中,typeof 和 instanceof 是用來(lái)判斷數(shù)據(jù)類型比較通用的兩個(gè)方法,這篇文章的目的是通過(guò)對(duì)這兩個(gè)方法介紹來(lái)分析其存在的不足并提出優(yōu)化方案2016-01-01
前端實(shí)現(xiàn)序列幀動(dòng)畫的幾種常見方法
這篇文章主要介紹了前端實(shí)現(xiàn)序列幀動(dòng)畫的多種方法,包括CSS動(dòng)畫、JavaScript控制、Canvas繪制、SVG動(dòng)畫和WebGL,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
微信小程序中實(shí)現(xiàn)雙向綁定的實(shí)戰(zhàn)過(guò)程
最近在小程序的開發(fā)過(guò)程中,需要用到雙向綁定,遇到報(bào)錯(cuò)才知道微信本身是不支持對(duì)象雙向綁定的,折騰一番找到解決方案,下面這篇文章主要給大家介紹了關(guān)于微信小程序中實(shí)現(xiàn)雙向綁定的相關(guān)資料,需要的朋友可以參考下2023-01-01
Bootstrap滾動(dòng)監(jiān)聽(Scrollspy)插件詳解
滾動(dòng)監(jiān)聽插件是用來(lái)根據(jù)滾動(dòng)條所處在的位置自動(dòng)更新導(dǎo)航項(xiàng)目, 顯示導(dǎo)航項(xiàng)目高亮顯示。這篇文章主要介紹了Bootstrap滾動(dòng)監(jiān)聽(Scrollspy)插件的相關(guān)資料,需要的朋友可以參考下2016-04-04

