GithubHelp home page GithubHelp logo

harshalshende / andala Goto Github PK

View Code? Open in Web Editor NEW

This project forked from uhleeshuh/andala

0.0 1.0 0.0 661 KB

A drawing web app for symmetric designs

HTML 25.25% JavaScript 50.12% CSS 24.63%

andala's Introduction

andala

About

andala is a drawing web application which allows users to create beautiful, symmetrical designs. The app is powered through vanilla JavaScript DOM manipulation of an HTML canvas element.

Access the live site here.

Example andala drawing

How to use andala

  • Choose your initial drawing preferences at the left of the page
  • Begin drawing by clicking into the canvas
  • Change your drawing preferences at any time and continue drawing
  • Clear your canvas with the button at the right

Technical Implementation

The two most complicated components of the application are:

  1. Its canvas auto-calibration relative to user screen size & scroll
  2. Its real-time symmetry calculation via conversion of the canvas grid into a Cartesian coordinate plane

Canvas auto-calibration

Since an HTML canvas does not provide mouse coordinates when a user clicks over it, I dynamically calculated canvas coordinates from full window MouseEvent coordinates via the getCanvasCoords function. This translates the window's mouse coordinates into canvas coordinates which are agnostic to screen size and window scroll. The result is a precise drawing experience for the user even if s/he scrolls or changes screen size during a drawing session.

// from canvas.js

getCanvasCoords(){
  const canvasPosition = this.canvasElement.getBoundingClientRect();
  const canvasLeft = canvasPosition.left + window.scrollX;
  const canvasTop = canvasPosition.top + window.scrollY;
  return [canvasLeft, canvasTop];
}

Cartesian canvas symmetric calculations

Since an HTML canvas begins its coordinate system with (0,0) at the top left corner, I translated the canvas grid into a Cartesian plane. When a user clicks anywhere on the canvas, the distance from the origin is calculated as well as a set of symmetric point(s) which will be simultaneously drawn.

In the case of radial symmetry, the first user click calculates an initial angle theta about the origin. This, in conjunction with the radialOrder -- or number of symmetry slices -- is used to determine equidistantly-spaced points in radians whose canvas coordinates are calculated using trigonometry.

// from canvas.js

computeRadialSymPairs(e){
  const symmetricPairSet = [];

  const xDistance = (e.pageX - this.canvasLeftSide() - this.axisPoint[0]);
  const yDistance = -(e.pageY - this.canvasTop() - this.axisPoint[1]);
  const pythagoreanSum = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
  const radius =  Math.sqrt(pythagoreanSum);

  let theta;
  if (xDistance > 0 && yDistance >= 0){
    // first quadrant
    theta = Math.atan(yDistance / xDistance);
  } else if (xDistance <= 0 && yDistance >= 0){
    // second quadrant
    theta = Math.PI - Math.asin(yDistance / radius);
  } else if (xDistance < 0 && yDistance <= 0){
    // third quadrant
    theta = Math.PI + Math.atan(yDistance / xDistance);
  } else if (xDistance >= 0 && yDistance <= 0){
    // fourth quadrant
    theta = (2 * Math.PI) - Math.acos(xDistance / radius);
  }

  const sliceSizeRadians = (2 * Math.PI) / this.radialOrder;

  const thetaPrimes = [];
  //note: thetaPrimes will not include theta since firstPair coordinates are already known from the user's click
  for (let i = 1; i <= this.radialOrder; i ++){
    thetaPrimes.push(theta + (sliceSizeRadians * i));
  }

  thetaPrimes.forEach(angle => {
    const canvasX = (radius * Math.cos(angle)) + this.axisPoint[0];
    const canvasY = this.axisPoint[1] - (radius * Math.sin(angle));
    symmetricPairSet.push([canvasX, canvasY]);
  });
  return { symmetricPairSet };
}

Future Features

In the future, I plan to add the following features:

  • User may customize canvas size
  • User may undo a previous action
  • User may draw with circles and other shapes in addition to line drawing

andala's People

Contributors

dependabot[bot] avatar fdalvi avatar uhleeshuh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.