Tuesday, August 15, 2017

ArtAtomic - 2D Geometry and Color library for JavaSript


When doing 2D graphics there are a few patterns you'll use all the time - points, rectangles, matrices, etc... To make 2D geometry and colors easy to work with, use ArtAtomic. The atomic data-types include: Point, Color, Matrix, Rectangle and Perimeter. ArtAtomic provides the foundation for other ArtSuite libraries including ArtEngine, ArtText and ArtBitmap.

Quick Example

p1      = point 10,  20
offset  = point 300, 400
area    = rect  200, 200, 1000, 500

area.contains p1                       # false
area.contains p1.add offset            # true!

red   = rgbColor #f00
blue  = red.withHue 2/3
white = red.withSat 0
red2  = hslColor 0, 1, 1
red3  = hslColor 1, 1, 1
red2.eq red                            # true!
red3.eq red                            # true!

halfTransparentRed = rgbColor #f007
halfTransparentRed.eq red.withAlpha .5 # true!

What Does Atomic Mean?

These classes are designed to be used in a pure-functional way. Never modify their properties. Instead, use their methods to generate new objects.

HOWEVER, JavaScript performance requires minimizing the number of objects you create. As-such, there are some ways to end-run the pure-functional nature of ArtAtomic by re-using existing same-type objects. This should only be done in places where you can prove that there are no other references to the object you are reusing. In general, the way this works in ArtAtomic is some methods take an extra, optional argument called "into" which lets you specify where the result should be placed. If that argument is not provided, "into" is set to a new object. In either case, "into" is the value returned from these functions.

Color (primary props: r, g, b, a)

The Color class provides many tools for manipulating colors. It works in RGBA and HSLA color-spaces. It supports converting to and from many standard string-formats: "#FFF" and "rgb(255, 255, 255)" for starters. 

One simple, cool example: rgbColor("#f00").withHue(2/3) == "#00f"

Point (primary props: x, y)

Points are the foundation of any geometry framework. These points are all 2D: x & y. This library lets you perform math and comparisons on points: point(1, 2).add(point(3,4)).average(). You can treat these points as vectors: dot and cross-products are: point1.dot(point2) or point1.cross(point2). Also, point2.magnitude will return the magnitude of a point.

Rectangle (primary props: x, y, w, h)

Rectangles specify their upper-left-hand corner and their width and height. Strictly speaking, the upper-left point is included in the rectangle and the lower-right point is not: rect(1,2,3,4).contains(point 1, 2) - true, but rect(1,2,3,4).contains(point 4, 6) is false.

Rectangle has many useful methods in addition to basic arithmetic and comparison including: union, intersection, cutout, nearestInsidePoint and more...

Matrix (primary props: tx, ty, sx, sy, shx, shy)

This 2D matrix library provides all the standard matrix operations: translate, scale, rotate, mul(tiply), invert and, most importantly, myMatrix.transform(myPoint) => myTransformedPoint. 

Perimeter (primary props: left, right, top, bottom)

The most recent addition to ArtAtomic is Perimeter. This is primarily used to express margins and padding in ArtEngine. Currently there are only a few additional methods on top of the standard arithmetic and comparison operations.

ArtAtomic Resources

Learn more on the wiki: ArtAtomic Wiki

8 comments:

  1. The wonderful 2D geometry picture with the perfect guide of the coding, this will really help the civil and architecture engineers. Wonderful post for the engineers.

    ReplyDelete
  2. fantastic publish, very informative. I ponder why the other specialists of this sector don't realize this. You should proceed your writing. I'm sure, you've a great readers' base already! facebook log in facebook

    ReplyDelete
  3. Most taxes and costs are set on the provincial, or even municipal level. canada mortgage calculator Investment property loans may also be used by house flippers who renovate then sell houses within the market. mortgage payment calculator

    ReplyDelete
  4. You have outdone yourself this time. It is probably the best, most short step by step guide that I have ever seen. colorization of black and white photos

    ReplyDelete
  5. JavaScript is a programming language that allows you to make web pages interactive. Our best and luxury Party Bus Services Southampton NY that can give you best experience.

    ReplyDelete
  6. I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.It’s time to must know about it Religious Extremism for more details.

    ReplyDelete
  7. On this subject internet page, you'll see my best information, be sure to look over this level of detail. Satta king online

    ReplyDelete
  8. บริการเกมสล็อตออนไลน์ปี 2022 เกมให้เลือกเล่นมากกว่า 1,000 เกมsuperslot เครดิตฟรี 20 สล็อตออนไลน์ ซุปเปอร์สล็อต ซุปเปอร์สล็อต78

    ReplyDelete

Note: Only a member of this blog may post a comment.