Skip to content

Instantly share code, notes, and snippets.

@boustrophedon
Created March 6, 2019 23:27
Show Gist options
  • Save boustrophedon/f48dd76c02e1ebfa2a577627c48d0dd8 to your computer and use it in GitHub Desktop.
Save boustrophedon/f48dd76c02e1ebfa2a577627c48d0dd8 to your computer and use it in GitHub Desktop.
mouse-controlled quadratic
import 'dart:html';
main() async {
CanvasElement canvas = querySelector("canvas");
canvas.width = 500;
canvas.height = 500;
var ctx = canvas.context2D;
var mousepos = Point<num>(250.0, 250.0);
void handle_mouse(MouseEvent e) {
mousepos = e.offset;
}
canvas.onMouseMove.listen(handle_mouse);
var start = [0, 250];
var end = [500, 250];
while (true) {
ctx.fillStyle = 'green';
ctx.fillRect(0, 0, 500, 500);
var path = Path2D();
path.moveTo(start[0], start[1]);
path.quadraticCurveTo(mousepos.x, mousepos.y, end[0], end[1]);
ctx.strokeStyle = 'black';
ctx.lineWidth = 10;
ctx.stroke(path);
ctx.fillStyle = 'red';
ctx.fillRect(mousepos.x-5, mousepos.y-5, 10, 10);
await window.animationFrame;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment