export default function canvasBg(){ window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; })(); var c = document.getElementById('canvas'); var $ = c.getContext('2d'); var w = c.width = window.innerWidth; var h = c.height = window.innerHeight; var _w = w * 0.5; var _h = h * 0.5; var arr = []; var cnt = 0; window.addEventListener('load', resize); window.addEventListener('resize', resize, false); function resize() { c.width = w = window.innerWidth; c.height = h = window.innerHeight; c.style.position = 'absolute'; c.style.left = (window.innerWidth - w) * .01 + 'px'; c.style.top = (window.innerHeight - h) * .01 + 'px'; } function anim() { cnt++; if (cnt % 6) draw(); window.requestAnimFrame(anim); } anim(); function draw() { var splot = { x: rng(_w - 900, _w + 900), y: rng(_h - 900, _h + 900), r: rng(20, 80), spX: rng(-1, 1), spY: rng(-1, 1) }; arr.push(splot); while (arr.length > 100) { arr.shift(); } $.clearRect(0, 0, w, h); for (var i = 0; i < arr.length; i++) { splot = arr[i];; $.fillStyle = rndCol(); $.beginPath(); $.arc(splot.x, splot.y, splot.r, 0, Math.PI * 2, true); $.shadowBlur = 80; $.shadowOffsetX = 2; $.shadowOffsetY = 2; $.shadowColor = rndCol(); $.globalCompositeOperation = 'lighter'; $.fill(); splot.x = splot.x + splot.spX; splot.y = splot.y + splot.spY; splot.r = splot.r * 0.96; } } function rndCol() { var r = Math.floor(Math.random() * 180); var g = Math.floor(Math.random() * 60); var b = Math.floor(Math.random() * 100); return "rgb(" + r + "," + g + "," + b + ")"; } function rng(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } }
时间: 2024-03-30 14:38:58 浏览: 52
这是一个用 JavaScript 实现的 canvas 背景效果,它会在 canvas 元素上绘制一些圆形的粒子,并给这些粒子添加阴影和颜色渐变,形成一个动态的背景效果。具体实现方式是通过 setInterval() 方法不断调用 draw() 函数来更新画布。在 anim() 函数中通过 requestAnimationFrame() 方法来循环调用 draw() 函数,以达到动态更新的效果。resize() 函数会在页面大小发生改变时调用,重新设置 canvas 元素的宽高和位置。rng() 函数用来生成一个指定范围内的随机数,rndCol() 函数用来生成一个随机的颜色值。
相关问题
请根据代码片段仿写实现div左下角拖拽移动用具体代码实现,import Vue from 'vue' Vue.directive('dialogZoomOut', { bind(el, binding, vnode, oldVnode) { let minWidth = 400;let minHeight = 300;let isFullScreen = false; let nowWidth = 0;let nowHight = 0;let nowMarginTop = 0;const dialogHeaderEl = el.querySelector('.el-dialog__header');const dragDom = el.querySelector('.el-dialog');dragDom.style.overflow = "auto";dialogHeaderEl.onselectstart = new Function("return false");dialogHeaderEl.style.cursor = 'move';const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);let moveDown = (e) => {const disX = e.clientX - dialogHeaderEl.offsetLeft;const disY = e.clientY - dialogHeaderEl.offsetTop;let styL, styT;if (sty.left.includes('%')) {styL = +document.body.clientWidth * (+sty.left.replace(/%/g, '') / 100);styT = +document.body.clientHeight * (+sty.top.replace(/%/g, '') / 100);} else {styL = +sty.left.replace(/px/g, '');styT = +sty.top.replace(/px/g, '');};document.onmousemove = function (e) {const l = e.clientX - disX;const t = e.clientY - disY;dragDom.style.left = `${l + styL}px`;dragDom.style.top = `${t + styT}px`;};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}dialogHeaderEl.onmousedown = moveDown;dialogHeaderEl.ondblclick = (e) => {if (isFullScreen == false) {nowHight = dragDom.clientHeight;nowWidth = dragDom.clientWidth;nowMarginTop = dragDom.style.marginTop;dragDom.style.left = 0;dragDom.style.top = 0;dragDom.style.height = "100VH";dragDom.style.width = "100VW";dragDom.style.marginTop = 0;isFullScreen = true;dialogHeaderEl.style.cursor = 'initial';dialogHeaderEl.onmousedown = null;} else {dragDom.style.height = "auto";dragDom.style.width = nowWidth + 'px';dragDom.style.marginTop = nowMarginTop;isFullScreen = false;dialogHeaderEl.style.cursor = 'move';dialogHeaderEl.onmousedown = moveDown;}}let resizeEl = document.createElement("div");dragDom.appendChild(resizeEl);resizeEl.style.cursor = 'se-resize';resizeEl.style.position = 'absolute';resizeEl.style.height = '10px';resizeEl.style.width = '10px';resizeEl.style.right = '0px';resizeEl.style.bottom = '0px';resizeEl.style.zIndex = '99';resizeEl.onmousedown = (e) => {let clientX = e.clientX;let disX = e.clientX - resizeEl.offsetLeft;let disY = e.clientY - resizeEl.offsetTop;document.onmousemove = function (e) {e.preventDefault(); let x = e.clientX - disX + (e.clientX - clientX);let y = e.clientY - disY;dragDom.style.width = x > minWidth ? `${x}px` : minWidth + 'px';dragDom.style.height = y > minHeight ? `${y}px` : minHeight + 'px';};document.onmouseup = function (e) {document.onmousemove = null;document.onmouseup = null;};}}})
以下是仿写的代码实现:
```html
<template>
<div class="dialog-wrapper" v-dialog-zoom-out>
<div class="el-dialog">
<div class="el-dialog__header">Dialog Title</div>
<div class="el-dialog__body">Dialog Content</div>
<div class="el-dialog__footer">
<button @click="$emit('close')">Close</button>
</div>
<div class="el-dialog__resize"></div>
</div>
</div>
</template>
<script>
export default {
name: "Dialog",
};
</script>
<style>
.dialog-wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.el-dialog {
position: absolute;
top: 0;
left: 0;
overflow: auto;
max-height: 80vh;
background-color: #fff;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.el-dialog__header {
padding: 16px;
background-color: #f5f7fa;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
cursor: move;
}
.el-dialog__body {
padding: 16px;
}
.el-dialog__footer {
display: flex;
justify-content: flex-end;
padding: 16px;
background-color: #f5f7fa;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
.el-dialog__footer button {
padding: 8px 16px;
background-color: #fff;
border: 1px solid #c1c1c1;
border-radius: 4px;
cursor: pointer;
}
.el-dialog__resize {
position: absolute;
bottom: 0;
right: 0;
width: 10px;
height: 10px;
cursor: se-resize;
background-color: #c1c1c1;
}
</style>
```
```javascript
import Vue from "vue";
Vue.directive("dialogZoomOut", {
bind(el, binding, vnode, oldVnode) {
let minWidth = 400;
let minHeight = 300;
let isFullScreen = false;
let nowWidth = 0;
let nowHight = 0;
let nowMarginTop = 0;
const dialogHeaderEl = el.querySelector(".el-dialog__header");
const dragDom = el.querySelector(".el-dialog");
dragDom.style.overflow = "auto";
dialogHeaderEl.onselectstart = new Function("return false");
dialogHeaderEl.style.cursor = "move";
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
let moveDown = (e) => {
const disX = e.clientX - dialogHeaderEl.offsetLeft;
const disY = e.clientY - dialogHeaderEl.offsetTop;
let styL, styT;
if (sty.left.includes("%")) {
styL = +document.body.clientWidth * (+sty.left.replace(/%/g, "") / 100);
styT = +document.body.clientHeight * (+sty.top.replace(/%/g, "") / 100);
} else {
styL = +sty.left.replace(/px/g, "");
styT = +sty.top.replace(/px/g, "");
}
document.onmousemove = function (e) {
const l = e.clientX - disX;
const t = e.clientY - disY;
dragDom.style.left = `${l + styL}px`;
dragDom.style.top = `${t + styT}px`;
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
dialogHeaderEl.onmousedown = moveDown;
dialogHeaderEl.ondblclick = (e) => {
if (isFullScreen == false) {
nowHight = dragDom.clientHeight;
nowWidth = dragDom.clientWidth;
nowMarginTop = dragDom.style.marginTop;
dragDom.style.left = 0;
dragDom.style.top = 0;
dragDom.style.height = "100VH";
dragDom.style.width = "100VW";
dragDom.style.marginTop = 0;
isFullScreen = true;
dialogHeaderEl.style.cursor = "initial";
dialogHeaderEl.onmousedown = null;
} else {
dragDom.style.height = "auto";
dragDom.style.width = nowWidth + "px";
dragDom.style.marginTop = nowMarginTop;
isFullScreen = false;
dialogHeaderEl.style.cursor = "move";
dialogHeaderEl.onmousedown = moveDown;
}
};
let resizeEl = document.createElement("div");
dragDom.appendChild(resizeEl);
resizeEl.style.cursor = "se-resize";
resizeEl.style.position = "absolute";
resizeEl.style.height = "10px";
resizeEl.style.width = "10px";
resizeEl.style.right = "0px";
resizeEl.style.bottom = "0px";
resizeEl.style.zIndex = "99";
resizeEl.onmousedown = (e) => {
let clientX = e.clientX;
let disX = e.clientX - resizeEl.offsetLeft;
let disY = e.clientY - resizeEl.offsetTop;
document.onmousemove = function (e) {
e.preventDefault();
let x = e.clientX - disX + (e.clientX - clientX);
let y = e.clientY - disY;
dragDom.style.width = x > minWidth ? `${x}px` : minWidth + "px";
dragDom.style.height = y > minHeight ? `${y}px` : minHeight + "px";
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
};
},
});
```
上述代码实现了一个类似于Element UI中的Dialog组件的拖拽和缩放功能。在使用时,只需要将 `v-dialog-zoom-out` 指令绑定到Dialog组件的容器元素上,即可实现拖拽和缩放功能。
怎么优化这段代码export default { data() { return { goodsId: " ", goodsData: [], current: 0, oldScrollTop: 0, scrollTop: 0, searchBgc: " rgba(0, 0, 0, 0.1)", show: false, opacity: 0, flag: true, }; }, mounted() { this.handleScroll(); }, beforeDestroy() { window.removeEventListener("scroll", this.handleScrollFn); //离开当前组件别忘记移除事件监听哦 }, watch: { scrollTop: { handler: function (newValue, oldValue) { clearTimeout(this.timer); this.timer = setTimeout(() => { if (newValue == window.scrollY) { this.oldScrollTop = newValue; if (this.oldScrollTop > 100) { this.show = true; } else { this.show = false; } } else { if (this.oldScrollTop == oldValue) { // console.log("滚动开始"); } } }, 20); }, immediate: true, }, }, methods: { handleScroll() { this.handleScrollFn = () => { this.scrollTop = window.scrollY; }; window.addEventListener("scroll", this.handleScrollFn); }, onClickLeft() { this.$router.go(-1); }, onChange(index) { this.current = index; }, returnFn() { this.$router.go(-1); }, // 加入购物车 joinShopCar() { alert("加入购物车成功"); let shopcarList = JSON.parse(localStorage.getItem("shopcar")) || []; console.log(shopcarList); this.goodsData.num = 1; this.goodsData.isChecked = false; let ind = shopcarList.findIndex((ele) => { return ele.id == this.goodsId; }); console.log(ind); // 如果有相同的返回该数据的下标,没有则返回-1 if (ind != -1) { shopcarList[ind].num++; } else { shopcarList.push(this.goodsData); } localStorage.setItem("shopcar", JSON.stringify(shopcarList)); this.$router.push("/shopcar"); }, // 立即结算 payFn() { localStorage.setItem("goodspay", JSON.stringify(this.goodsData)); this.$router.push("/pay"); }, }, async created() { this.goodsId = this.$route.query.id; try { let { data } = await getGoodsDetailApi(this.goodsId); this.goodsData = data.data.goodsData; console.log(this.goodsData); } catch (err) { console.log(err); } }, };
这段代码可以考虑进行以下优化:
1. 将方法中的注释删除,因为这些注释并没有提供太多有用的信息,反而会干扰代码的阅读。
2. 将所有的变量定义在最上面,以便于阅读和维护。同时,可以将一些不经常使用的变量定义为局部变量,以减少内存的占用。
3. 将一些方法提取出来,封装成单独的函数并放在外部,以提高代码的可复用性和可读性。
4. 为了提高代码的性能,可以将一些异步操作放到 created 钩子函数中,以免影响页面的渲染速度。
5. 对于方法中的一些重复代码,可以进行优化,如将相同部分提取出来,避免重复代码的出现。
6. 对于一些可能产生副作用的操作,如 alert(),应该尽量避免在代码中使用,以免影响用户体验。
下面是优化后的代码:
```
<template>
<!-- 模板代码 -->
</template>
<script>
import { getGoodsDetailApi } from "@/api"; // 导入 api 接口
export default {
data() {
return {
goodsId: " ",
goodsData: [],
current: 0,
oldScrollTop: 0,
scrollTop: 0,
searchBgc: "rgba(0, 0, 0, 0.1)",
show: false,
opacity: 0,
flag: true,
timer: null,
};
},
mounted() {
this.handleScroll();
},
beforeDestroy() {
window.removeEventListener("scroll", this.handleScrollFn);
},
watch: {
scrollTop: {
handler: function (newValue, oldValue) {
clearTimeout(this.timer);
this.timer = setTimeout(() => {
if (newValue == window.scrollY) {
this.oldScrollTop = newValue;
this.show = this.oldScrollTop > 100;
} else {
if (this.oldScrollTop == oldValue) {
// 滚动开始
}
}
}, 20);
},
immediate: true,
},
},
methods: {
// 监听滚动事件
handleScroll() {
this.handleScrollFn = () => {
this.scrollTop = window.scrollY;
};
window.addEventListener("scroll", this.handleScrollFn);
},
// 返回上一页
onClickLeft() {
this.$router.go(-1);
},
// 切换商品详情页
onChange(index) {
this.current = index;
},
// 返回上一页
returnFn() {
this.$router.go(-1);
},
// 加入购物车
joinShopCar() {
if (confirm("确定要加入购物车吗?")) {
let shopcarList = JSON.parse(localStorage.getItem("shopcar")) || [];
this.goodsData.num = 1;
this.goodsData.isChecked = false;
let ind = shopcarList.findIndex((ele) => {
return ele.id == this.goodsId;
});
if (ind != -1) {
shopcarList[ind].num++;
} else {
shopcarList.push(this.goodsData);
}
localStorage.setItem("shopcar", JSON.stringify(shopcarList));
this.$router.push("/shopcar");
}
},
// 立即结算
payFn() {
localStorage.setItem("goodspay", JSON.stringify(this.goodsData));
this.$router.push("/pay");
},
// 获取商品详情数据
async getGoodsDetail() {
try {
let { data } = await getGoodsDetailApi(this.goodsId);
this.goodsData = data.data.goodsData;
console.log(this.goodsData);
} catch (err) {
console.log(err);
}
},
},
created() {
this.goodsId = this.$route.query.id;
this.getGoodsDetail();
},
};
</script>
```
阅读全文