Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mirabledictu opened this issue Jun 28, 2021 · 1 comment

Comments

@mirabledictu
Copy link

mirabledictu commented Jun 28, 2021

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
Copy link
Collaborator

pshihn commented Nov 3, 2021

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() })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants