Pre-graphics

Defer

  1. <script src="file.js" type="module" defer></script>
  • load the module from the file
  • dont execute until the page is loaded

    window.requestsAnimationFrame(func);

  1. this puts an event on the event queue
  2. it will be some time in the future(nexr frame)
  3. it will happen after current function finishes

Time delta

  1. <script type="module">
  2. /* Slider 2 - measure the time between iterations */
  3. let lasttime; // time
  4. /** @type{HTMLInputElement} */ let slr = (/** @type{HTMLInputElement} */ document.getElementById("slider2"));
  5. function advanceSLR(timestamp) {
  6. let newValue;
  7. if (lasttime === undefined) {
  8. newValue = 0;
  9. } else {
  10. // doing this in steps to explain...
  11. const delta = (timestamp - lasttime); // how many milliseconds since last update
  12. const change = delta / 1000.0 * 100.0; // want the slider in 1000ms, but the slider is 100 units
  13. newValue = (Number(slr.value)+change) % 100; // move the slider forward
  14. }
  15. slr.value = newValue.toString();
  16. window.requestAnimationFrame(advanceSLR);
  17. lasttime = timestamp; // remember the last update
  18. }
  19. window.requestAnimationFrame(advanceSLR);
  20. </script>
  1. <script type="module">
  2. /* Slider 6 - measure the time between iterations */
  3. /* this is similar to slider 2, except that we store the "current value"
  4. ourselves, so we are not subject to rounding errors
  5. */
  6. let lasttime; // time
  7. let value = 0;
  8. /** @type{HTMLInputElement} */ let slr = (/** @type{HTMLInputElement} */ document.getElementById("slider6"));
  9. function advanceSLR(timestamp) {
  10. if (! (lasttime === undefined)) {
  11. const delta = (timestamp - lasttime) / 10.0;
  12. value = (value + delta) % 100;
  13. }
  14. slr.value = value.toString();
  15. window.requestAnimationFrame(advanceSLR);
  16. lasttime = timestamp; // remember the last update
  17. }
  18. window.requestAnimationFrame(advanceSLR);
  19. </script>

Basic graphics

Representing images types

image.png

  1. Sampled(raster)
    1. LCD/LED/CRT
    2. laser printer, inkjet printer
    3. film, irregular grid of crystals
  2. Geometric(primitives)

    1. pen plotter
    2. laser light shows
    3. old fashioned vector display

      continuous vs. flicker/strobe

  3. flicker fusion: flashing things seem continues

  4. draw too slowly: double buffering, draw in back

    Even / Odd and Winding(Non-zero)

    image.pngimage.png
    Even/Odd:
  • Even(includes) -> outside
  • Odd -> inside

Winding(non-zero):

    • 1 for clockwise, -1 for counter-clockwise
  • total = 0 -> inside,

    Web graphics APIs

  1. Canvas 2D
    1. an immediate mode 2d drawing library, 1 DOM element for picture
  2. SVG(scalable vector graphics)
    1. a display-list(object-based) graphics library
    2. grphics objects are DOM elements
  3. WebGL
    1. direct access to the graphics hardware
    2. require low-level control

Immediate vs. Retained APIs

Canvas is the immediate API, while SVG is the retained API

2D transformation

translate and scale (order matters!)

context.scale(sx, sy); -> scale
context.translate(2,2); -> translate

  1. scale and translate

context.scale(sx,sy);
context.translate(tx,ty);
context.lineTo(X,Y);
means for all X, Y:
context.lineTo(sx(X + tx), sy(Y + ty))

  1. translate and scale

context.translate(tx,ty);
context.scale(sx,sy);
context.lineTo(X,Y);
means for all X, Y:
context.lineTo(tx+(Xsx), ty+(Ysy))

rotation

image.png
rotate around a point:
context.translate(cx,cy);
context.rotate(angle);
context.translate(-cx, -cy);
drawThing();

skew (shear)

Function composition

x= **h**( **g**( f(**x**))) -> x= (h o g o f)(x)

Homogeneous Coordinates

translate:
[1 0 1 [x [x + 1
0 1 1 y y + 1
0 0 1 ] * 1] = 1]
scale:
[2 0 0
0 2 0
0 0 1]
ATTENTION:
[ 1 0 0
0 1 0
0 0 2 ]
->
[ x/2
y/2
1 ]
2 here means scale by 1/2


Curves

Curves 1

C(n) continuous
C(0) - position, no gaps
C(1) - positions and tangents, no corners, G1 only the direction, but C1 both direction and size
C(2) - positions and tangents and 2nd derivatives (切线大小一样), if C(2), almost must G(2)


Curves 2

Cardinal Cubics - C(1)

image.png

Cardinal Splines

The trick to cardinal splines is that we compute the derivatives at any point based on the previous and next point.
s = (1-t) / 2
p′i=s*(pi+1−pi−1), when s = 1/2, we call this cardinal splines

DeCastlejau Constructions

We ues DeCastlejau Constructions to get point position in any position.(when u = what?)
image.png
Simple example: [0,8,16,24]
[4,12,20]
[8,16]
[12]
We get the final x value as 12.

Cubic Bézier Curves

image.png
image.png
f′(0)=h′0=3(p1−p0)f′(0)=h0′=3(p1−p0)
the tangent is the difference in points times 2 for quadratic

Example:
A curve is made from two Bezier Segments that meet with C(1) continuity.
The first segment is a cubic and has its control points at (0,0), (0,2), (4,6), and (6,6)
The second segment is a quadratic, and has its control points at (6,6), (9,6), (12,0)

the first control point must be the same as the last control point of the previous segment
We know the tangent vector where the curve meets must be 3 ( (6,6)-(4,6) ) or (6,0). So, to have that tangent vector, the quadratic must have its second point at (9,6). (the tangent is the difference in points times 2).


Curves 3

Beziers

Good:

  1. interpolate end-points, stay in convex hull
  2. tangents at ends based on points

Not Good:

  1. not projective invariant
  2. cannot represent conics 圆锥曲线, circle
  3. sometimes we want interpolation

    Cardinals (local, next and prev.)

  4. C(1)

  5. local
  6. interpolating

    Natural Splines (Cubics) (not local)

  7. C(2)

  8. second derivative is zero at the ends
  9. not local

    B-Splines (local) - C(d - 1)

  10. d = 1, line segments [p0,p1], [p1,p2],…[pn-1,pn]

  11. d = 2, quadratics [p0, p1, p2], [p1, p2, p3],..[pn-2, pn-1, pn]
  12. uniform low order polynomial, d = 2 or 3 in graphics
  13. Cubic B-Splines:
    1. 4 points influence a curve segment(locally),
    2. neighboring segments share 3 points
    3. each segment is a cubric polynomial
  14. Locality: each control point influences d+1 segments
  15. Continuity: curve is C(d - 1)
  16. not interpolating