gpt4 book ai didi

javascript - 我需要让这个 Canvas 透明,但没有一个解决方案有效

转载 作者:行者123 更新时间:2023-11-27 23:57:18 25 4
gpt4 key购买 nike

我已经尝试了所有的 jQuery 技巧和 CSS 技巧,但它们都不起作用。我对 <canvas> 一无所知。但我已经尝试解决这个问题一天了。有人可以帮忙吗?

我尽量不弄乱代码,只是让黑色背景透明,这样我就可以在狂欢中使用这个 Canvas ..

//辅助函数

const PI2 = Math.PI * 2
const random = (min, max) => Math.random() * (max - min + 1) + min | 0
const timestamp = _ => new Date().getTime()

// container
class Birthday {
constructor() {
this.resize()

// create a lovely place to store the firework
this.fireworks = []
this.counter = 0

}

resize() {
this.width = canvas.width = window.innerWidth
let center = this.width / 2 | 0
this.spawnA = center - center / 4 | 0
this.spawnB = center + center / 4 | 0

this.height = canvas.height = window.innerHeight
this.spawnC = this.height * .1
this.spawnD = this.height * .5

}

onClick(evt) {
let x = evt.clientX || evt.touches && evt.touches[0].pageX
let y = evt.clientY || evt.touches && evt.touches[0].pageY

let count = random(3, 5)
for (let i = 0; i < count; i++) this.fireworks.push(new Firework(
random(this.spawnA, this.spawnB),
this.height,
x,
y,
random(0, 260),
random(30, 110)))

this.counter = -1

}

update(delta) {
ctx.globalCompositeOperation = 'hard-light'
ctx.fillStyle = `rgba(20,20,20,${ 7 * delta })`
ctx.fillRect(0, 0, this.width, this.height)

ctx.globalCompositeOperation = 'lighter'
for (let firework of this.fireworks) firework.update(delta)

// if enough time passed... create new new firework
this.counter += delta * 3 // each second
if (this.counter >= 1) {
this.fireworks.push(new Firework(
random(this.spawnA, this.spawnB),
this.height,
random(0, this.width),
random(this.spawnC, this.spawnD),
random(0, 360),
random(30, 110)))
this.counter = 0
}

// remove the dead fireworks
if (this.fireworks.length > 1000) this.fireworks = this.fireworks.filter(firework => !firework.dead)

}
}

class Firework {
constructor(x, y, targetX, targetY, shade, offsprings) {
this.dead = false
this.offsprings = offsprings

this.x = x
this.y = y
this.targetX = targetX
this.targetY = targetY

this.shade = shade
this.history = []
}
update(delta) {
if (this.dead) return

let xDiff = this.targetX - this.x
let yDiff = this.targetY - this.y
if (Math.abs(xDiff) > 3 || Math.abs(yDiff) > 3) { // is still moving
this.x += xDiff * 2 * delta
this.y += yDiff * 2 * delta

this.history.push({
x: this.x,
y: this.y
})

if (this.history.length > 20) this.history.shift()

} else {
if (this.offsprings && !this.madeChilds) {

let babies = this.offsprings / 2
for (let i = 0; i < babies; i++) {
let targetX = this.x + this.offsprings * Math.cos(PI2 * i / babies) | 0
let targetY = this.y + this.offsprings * Math.sin(PI2 * i / babies) | 0

birthday.fireworks.push(new Firework(this.x, this.y, targetX, targetY, this.shade, 0))

}

}
this.madeChilds = true
this.history.shift()
}

if (this.history.length === 0) this.dead = true
else if (this.offsprings) {
for (let i = 0; this.history.length > i; i++) {
let point = this.history[i]
ctx.beginPath()
ctx.fillStyle = 'hsl(' + this.shade + ',100%,' + i + '%)'
ctx.arc(point.x, point.y, 1, 0, PI2, false)
ctx.fill()
}
} else {
ctx.beginPath()
ctx.fillStyle = 'hsl(' + this.shade + ',100%,50%)'
ctx.arc(this.x, this.y, 1, 0, PI2, false)
ctx.fill()
}

}
}

let canvas = document.getElementById('birthday')
let ctx = canvas.getContext('2d')

let then = timestamp()

let birthday = new Birthday
window.onresize = () => birthday.resize()
document.onclick = evt => birthday.onClick(evt)
document.ontouchstart = evt => birthday.onClick(evt)

;
(function loop() {
requestAnimationFrame(loop)

let now = timestamp()
let delta = now - then

then = now
birthday.update(delta / 1000)


})()
<canvas id="birthday"></canvas>

最佳答案

您需要做的就是在 Birthday 类的 update 方法中将 fillRect 替换为 clearRect,例如这个:

ctx.clearRect(0, 0, this.width, this.height)

不过,您可能需要进一步调整烟花尾迹的颜色,因为它看起来应该与背景颜色相匹配。

如果您的 Canvas 仍然不透明,请检查您的 CSS 以确保您没有以这种方式设置背景颜色。

const PI2 = Math.PI * 2
const random = (min, max) => Math.random() * (max - min + 1) + min | 0
const timestamp = _ => new Date().getTime()

// container
class Birthday {
constructor() {
this.resize()

// create a lovely place to store the firework
this.fireworks = []
this.counter = 0

}

resize() {
this.width = canvas.width = window.innerWidth
let center = this.width / 2 | 0
this.spawnA = center - center / 4 | 0
this.spawnB = center + center / 4 | 0

this.height = canvas.height = window.innerHeight
this.spawnC = this.height * .1
this.spawnD = this.height * .5

}

onClick(evt) {
let x = evt.clientX || evt.touches && evt.touches[0].pageX
let y = evt.clientY || evt.touches && evt.touches[0].pageY

let count = random(3, 5)
for (let i = 0; i < count; i++) this.fireworks.push(new Firework(
random(this.spawnA, this.spawnB),
this.height,
x,
y,
random(0, 260),
random(30, 110)))

this.counter = -1

}

update(delta) {
ctx.globalCompositeOperation = 'hard-light'
ctx.fillStyle = `rgba(20,20,20,${ 7 * delta })`
ctx.clearRect(0, 0, this.width, this.height)

ctx.globalCompositeOperation = 'lighter'
for (let firework of this.fireworks) firework.update(delta)

// if enough time passed... create new new firework
this.counter += delta * 3 // each second
if (this.counter >= 1) {
this.fireworks.push(new Firework(
random(this.spawnA, this.spawnB),
this.height,
random(0, this.width),
random(this.spawnC, this.spawnD),
random(0, 360),
random(30, 110)))
this.counter = 0
}

// remove the dead fireworks
if (this.fireworks.length > 1000) this.fireworks = this.fireworks.filter(firework => !firework.dead)

}
}

class Firework {
constructor(x, y, targetX, targetY, shade, offsprings) {
this.dead = false
this.offsprings = offsprings

this.x = x
this.y = y
this.targetX = targetX
this.targetY = targetY

this.shade = shade
this.history = []
}
update(delta) {
if (this.dead) return

let xDiff = this.targetX - this.x
let yDiff = this.targetY - this.y
if (Math.abs(xDiff) > 3 || Math.abs(yDiff) > 3) { // is still moving
this.x += xDiff * 2 * delta
this.y += yDiff * 2 * delta

this.history.push({
x: this.x,
y: this.y
})

if (this.history.length > 20) this.history.shift()

} else {
if (this.offsprings && !this.madeChilds) {

let babies = this.offsprings / 2
for (let i = 0; i < babies; i++) {
let targetX = this.x + this.offsprings * Math.cos(PI2 * i / babies) | 0
let targetY = this.y + this.offsprings * Math.sin(PI2 * i / babies) | 0

birthday.fireworks.push(new Firework(this.x, this.y, targetX, targetY, this.shade, 0))

}

}
this.madeChilds = true
this.history.shift()
}

if (this.history.length === 0) this.dead = true
else if (this.offsprings) {
for (let i = 0; this.history.length > i; i++) {
let point = this.history[i]
ctx.beginPath()
ctx.fillStyle = 'hsl(' + this.shade + ',100%,' + i + '%)'
ctx.arc(point.x, point.y, 1, 0, PI2, false)
ctx.fill()
}
} else {
ctx.beginPath()
ctx.fillStyle = 'hsl(' + this.shade + ',100%,50%)'
ctx.arc(this.x, this.y, 1, 0, PI2, false)
ctx.fill()
}

}
}

let canvas = document.getElementById('birthday')
let ctx = canvas.getContext('2d')

let then = timestamp()

let birthday = new Birthday
window.onresize = () => birthday.resize()
document.onclick = evt => birthday.onClick(evt)
document.ontouchstart = evt => birthday.onClick(evt)

;
(function loop() {
requestAnimationFrame(loop)

let now = timestamp()
let delta = now - then

then = now
birthday.update(delta / 1000)


})()
div {
height : 100vh;
width : 100vw;
display : flex;
flex-direction : center;
align-items : center;
justify-content : center;
}

canvas {
position : absolute;
top : 0;
left : 0;
}
<div>Content Underneath</div>
<canvas id="birthday"></canvas>

关于javascript - 我需要让这个 Canvas 透明,但没有一个解决方案有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com