xxxxxxxxxx
75
// Credits on original image-based, rotating brush: https://editor.p5js.org/aferriss/sketches/r1z1HZ1qf
// press 1-9 to select brushes
// mouse click to get new brush for selection
// @TODO optimize
const startingImages = [
'https://lexica-serve-encoded-images.sharif.workers.dev/md/16240fca-0ba6-4859-aa63-8405ecf74ce3',
'https://lexica-serve-encoded-images.sharif.workers.dev/sm/038c8959-1c55-468f-b165-7bb944e1ebb5',
'https://lexica-serve-encoded-images.sharif.workers.dev/sm/054aa2f6-fea8-472f-840b-8a57257d9e91',
'https://lexica-serve-encoded-images.sharif.workers.dev/sm/02a269e0-b2b0-4c16-8cce-964b977c92e3',
]
let imageBrushes = []
let currentBrushIndex = 0
const seedWords = ['acorn', 'chicken', 'fox', 'raccoon']
async function getNewBrush() {
// #1 fetch data from Lexica's API
const word = random(seedWords)
const res = await fetch(
`https://lexica.art/api/v1/search?q=${word}%20pattern`,
)
const json = await res.json()
// #2 Get a random pattern
const newPatternUrl = random(json.images).srcSmall
// #3 assign this pattern to the currentBrushIndex
imageBrushes[currentBrushIndex] = loadImage(newPatternUrl)
}
function setup() {
cnv = createCanvas(window.innerWidth, window.innerHeight)
frameRate(24)
imageBrushes = startingImages.map((url) => loadImage(url))
imageMode(CENTER)
colorMode(HSB)
background('white')
cnv.mouseClicked(getNewBrush)
}
function draw() {
// rotate through the hues
drawSoftBrush((millis() / 16) % 360, 100, 0.03)
}
function drawSoftBrush(col, brushSize, speed) {
if (!imageBrushes[currentBrushIndex]) {
return
}
const time = millis()
// tint our brush
tint(col, 70, 100)
push()
translate(mouseX, mouseY)
scale(sin((time * speed) / 12) + 2)
rotate(time * 0.001)
image(imageBrushes[currentBrushIndex], 0, 0, brushSize, brushSize)
pop()
}
function keyTyped() {
if (key == "s") {saveCanvas(cnv, "myCanvas", "jpg")}
else if (key && !Number.isNaN(key)) {
currentBrushIndex = Number(key)
if (!imageBrushes[currentBrushIndex]) {
getNewBrush()
}
} else if ((key = 's')) {
saveCanvas(cnv, 'myCanvas', 'jpg')
}
}