rough-stuff / rough

Create graphics with a hand-drawn, sketchy, appearance
http://roughjs.com
MIT License
19.88k stars 615 forks source link

How would someone draw a rectangle without repeating elements via mouse events? #184

Open mirabledictu opened 3 years ago

mirabledictu commented 3 years ago

Currently, this is how I do it

import rough from 'roughjs'

let startX, startY, endX, endY, node

const svg = document.getElementById('svg')
const roughSVG = rough.svg(svg);

function pointerMove(e) {
    if (!startX) return

    end.x = e.x
    end.y = e.y

    if (node) {
        svg.removeChild(node)
    }

    node = roughSVG.rectangle(startX, startY, endX - startX, endY - startY)
    svg.appendChild(node)
}

function pointerDown(e) {
    startX = e.x
    startY = e.y
}

function pointerUp(e) {
    endX = e.x
    endY = e.y

    if (node) {
        svg.removeChild(node)
    }

    node = roughSVG.rectangle(startX, startY, endX - startX, endY - startY)
    svg.appendChild(node)
}

This makes the rectangle have shaky animation. Is there a good example of this? Or am I missing a method?

pshihn commented 2 years ago

Every time you render a rectangle a new random rectangle is generated. If you want to have most consistent rendering, you should provide a seed number. Seed could be any integer but you can always ask rough to create a new seed for you.

node = roughSVG.rectangle(startX, startY, endX - startX, endY - startY, { seed: rough.newSeed() })