comic-bubbles
Version:
Animated comic bubbles - what else?
46 lines (35 loc) • 1.39 kB
JavaScript
import { BubbleDeviation, bubbleDeviations } from './objects/bubbleDeviation.mjs'
import { wait } from './helperFunctions.mjs'
import { getScene } from './sceneCache.mjs'
import { stopRequested } from '../index.mjs'
export function injectBubbleDeviations(sceneId) {
let scene = getScene(sceneId)
for (let i = 0; i < scene.bubCon.getBubbleCount(); i++) {
let bubble = scene.bubCon.bubble(i)
bubble.margin.left += bubbleDeviations[i].bodyX
bubble.margin.top += bubbleDeviations[i].bodyY
}
}
export async function randomizeBubbleDeviations(sceneId) {
if (stopRequested.has(sceneId)) return
await wait(200)
const randomItem = Math.floor(Math.random() * 5)
const ranX = (Math.floor(Math.random() * 3) - 1) * 1
let oldX = bubbleDeviations[randomItem].bodyX
let arrowFurtherLeft = randomItem == 0 || bubbleDeviations[0].bodyX > bubbleDeviations[randomItem].bodyX
let notBelowThres = oldX + ranX >= -10
let notAboveThres = oldX + ranX <= 10
let newValue = 0
if (!arrowFurtherLeft) {
newValue = oldX + 1
}
newValue = notBelowThres && notAboveThres ? oldX + ranX : oldX
bubbleDeviations[randomItem].bodyX = newValue
randomizeBubbleDeviations(sceneId)
}
export function generateBubbleDeviations(sceneId) {
for (let i = 0; i < 10; i++) {
let bubbleDeviation = new BubbleDeviation()
bubbleDeviations.push(bubbleDeviation)
}
}