Cursor Click
← All Butter documentationA mouse cursor comes in and clicks on a button. The timing of the click is controlled on the timeline, using createTimeSlider.
let click
let label
let buttonColor
function setup() {
createCanvas(400, 240)
click = createTimeSlider('Click', {
start: 800,
end: 1800,
fixedLength: false,
})
label = createInput('Subscribe')
buttonColor = createColorPicker('#ff6ec4')
}
function draw() {
clear()
const { start, end } = click.value()
const t = millis()
// The button is pressed in for a moment after the click
const pressed = t >= end && t < end + 150
push()
translate(width / 2, height / 2)
scale(pressed ? 0.94 : 1)
rectMode(CENTER)
noStroke()
fill(buttonColor.value())
rect(0, 0, 200, 64, 32)
fill(255)
textAlign(CENTER, CENTER)
textSize(24)
text(label.value(), 0, 0)
pop()
// The cursor slides in from the corner, slowing down as it arrives
const progress = constrain(map(t, start, end, 0, 1), 0, 1)
const eased = 1 - pow(1 - progress, 3)
drawCursor(
lerp(width + 20, width / 2 + 40, eased),
lerp(height + 20, height / 2 + 20, eased),
)
}
function drawCursor(x, y) {
push()
translate(x, y)
fill(255)
stroke(0)
strokeWeight(2)
triangle(0, 0, 0, 26, 18, 19)
pop()
}
This is what changing the timing looks like on the timeline:
The pill starts when the cursor appears and ends when it clicks. It is made with fixedLength: false, so the user can drag the start to change when the cursor appears and the end to change when it clicks. Times are in milliseconds, so they are compared with millis(), and scrubbing the timeline moves millis() and the cursor with it. Before start the cursor's progress is 0, which keeps it off the canvas, and after end it is 1, which keeps it on the button. To click more than once, swap the one time slider for createTimeSliders and loop over its values.