EDITORCLASSBLOGLOGINOpkleOpkle, the Editor for CreatorsInfiniteExpression,In New Forms.New ContentOpkle makes an editor so anyone can build for the web without code, and share it anywhere.Blending text and screen, we build tools that make the web about imagination, not code.Building Opkle Editor: Linear Algebra in the Enginedev12Building Opkle Editor: An InDesign-Style Layout Editordev13Building Opkle Editor: Text That Flows Across Pagesdev14Building Opkle Editor: EPUB and IDML as Editable Pagesdev15Building Opkle Editor: One Layout, EPUB and Printdev161112131415...12 Building Opkle Editor: Linear Algebra in the Engine

If Part 11 covered refining the Rust factory into a graphics engine, the next place to dig deeper was linear algebra. In a graphics editor, moving an image layer, rotating it, skewing it, changing its perspective to fit four corners, resampling pixels and turning a raster image into vector paths all look like different features on the surface. Inside the engine, though, they are described in nearly the same language: vectors, matrices, bases, coordinate systems, projective space, interpolation, least squares, normalization, and the numerical stability that comes with all of them.

So when you build a graphics editor properly, you can hide the math behind the UI, but you cannot avoid it inside the engine. The user just drags a handle, but the Rust core has to interpret that drag as a coordinate transform. The user just tilts an image, but internally a 3×3 homography is built, its inverse is computed, and a source coordinate is recomputed for every output pixel. The user just presses a button to convert to SVG, but internally a balance is at work between connected components of the pixel grid, boundary tracing, curve approximation and path precision.

This article is a follow-up to Part 11, then, but a record that goes a little further down toward the math. It does not lay out every core design; it is closer to a development log of what mathematical judgments I actually needed when building a graphics engine. The Opkle graphics editor could not be a mere canvas UI. It had to be an editing engine in which coordinates, pixels and vectors can move back and forth between one another.

Vector space

The first thing to sort out in the graphics engine was the fact that all data is, in the end, vectors. A pixel's RGB is a 3-dimensional vector, and RGBA is a 4-dimensional one. A point's position is a 2-dimensional vector, and lifting it to homogeneous coordinates for an affine transform makes it a 3-dimensional vector. The channel vector of a color adjustment, a brush's direction vector, a path segment's control points, even coordinates on a PDF page can all be seen as elements of some vector space.

Taking this view changes how you feel about the code. Changing one pixel is not just editing four numbers; it is a function that sends a vector in `R^4` to another vector. The whole image can be seen as a vector field laid on the `Z²` grid. A brush stroke is an operation that mixes the vector field inside a particular region with another vector field, and blur is closer to a linear operation that replaces the vector at each position with a weighted sum of the surrounding vectors.

Linearity is very important here. If an operation T satisfies `T(a x + b y) = a T(x) + b T(y)`, it becomes easy to compose and decompose. Convolution such as blur is a linear operation under ideal conditions. Adding image A and image B and then blurring has the same structure as blurring each and then adding. Of course, in a real engine complete linearity is broken by clamping, gamma, alpha and integer quantization. Even so, it is far more stable to take linear operations as the starting point of internal thinking.

Conversely, HSL conversion, gamma correction, thresholding and clamping are nonlinear. These nonlinear operations feel natural to the user, but they are sensitive to the order of composition. Blurring and then thresholding gives a completely different result from thresholding and then blurring. So in the graphics engine I had to keep distinguishing which processing is linear and which is nonlinear, and in what order they should be chained to look convincing.

Coordinate frame

In a graphics editor there is more than one coordinate system. There are screen coordinates, canvas coordinates, coordinates inside a layer, image pixel coordinates and page coordinates for export. The point the user grabs with the mouse lives in screen space, but what actually has to change may be geometry in layer local space. If you do not clearly separate these coordinate systems, the editor soon starts behaving strangely.

In linear algebra terms, each coordinate system can be thought of as a vector space with a different basis. Even for the same point p, the coordinate values differ depending on which basis you view it in. When the canvas is zoomed or scrolled, or a layer is rotated, the mouse coordinates cannot be used as image pixel coordinates as they are. You have to trace the transform matrices backward, bringing the screen point down to a document point and then to a layer-local point.

For this, it is best to unify affine transforms as 3×3 matrices. If you lift a 2D coordinate `(x, y)` to the homogeneous coordinate `(x, y, 1)`, translation, rotation, scale and shear can all be expressed as a single matrix product. For example, translation goes into the last column, while rotation and scale go into the upper-left 2×2 block. That way, applying several transforms in order can be composed as a matrix product.

What matters is the order of the matrix product. `ABp` means apply B to p first and then A. To the user, “moved and rotated” and “rotated and moved” can look similar, but the real results differ. In particular, to rotate around a layer's center you need three matrices: move to the origin, rotate, and move back. Only if the engine pins down this ordering of coordinate systems clearly will the UI's handle movements match the user's expectations.

Affine layer

Most basic layer manipulations in a graphics editor are affine transforms. Translation, rotation, scaling and skewing all send lines to lines and keep parallel lines parallel. As long as this condition holds, computing a layer's bounding box and handles is relatively clear-cut. A rectangle becomes a parallelogram, and the internal proportions are preserved.

In formula terms, an affine transform is `p' = A p + t`. Here A is a 2×2 matrix and t is a translation vector. The determinant of A tells you the area scale and orientation. As the determinant approaches 0, the transform degenerates. The image gets squashed to a line, or its inverse becomes hard to compute. So the graphics engine has to guard against situations where the scale gets too small or the determinant gets excessively small.

The 2×2 matrix A holds several meanings. A pure rotation matrix keeps the lengths of the two basis vectors and keeps them orthogonal. Scale changes the lengths of the basis vectors. Shear changes the angle between the basis vectors. What looks to the user in the UI like “twisted it slightly” is, internally, changing the direction and length of the basis vectors. The transform panel of a graphics editor is, after all, a tool for manipulating the basis.

Getting this structure right also makes undo/redo and export stable. Instead of overwriting the pixels on screen each time, you can store the original asset and the transform matrix separately. The user can rotate and scale many times, but internally the original and the transform information are kept as long as possible and rasterized only when necessary. That reduces the quality loss from repeated editing.

Projective geometry

But if a graphics editor deals only with affine transforms, its expressiveness soon hits a wall. To make a poster look pasted on a wall, to flatten an image shot at an angle, or to fit an image layer by freely placing four corners, you need a projective transform. From here on, parallel lines no longer have to stay parallel. It is the moment of moving from affine geometry to projective geometry.

A projective transform is expressed as a 3×3 matrix H in homogeneous coordinates. Lift a 2D point `(x, y)` to `(x, y, 1)`, compute `H[x, y, 1]^T`, and divide by the last component w to get `(x'/w, y'/w)` again. The moment w comes in is important here. In an affine transform the last component always stays like 1, but in a projective transform w varies with position. As a result, perspective appears, shrinking toward the distance.

In projective space, `(x, y, w)` and `(λx, λy, λw)` represent the same point. A coordinate is defined not by a single representative value but by a ratio. This view feels unfamiliar at first, but it is extremely convenient when handling perspective transforms. Points at infinity can be expressed, and vanishing points of parallel lines come inside the same mathematics. For a four-corner transform in a graphics editor to look natural, the engine has to handle this projective space exactly.

This is also why four point correspondences can determine a homography. A single 3×3 matrix has 8 degrees of freedom apart from scale. The condition of sending four points of a quadrilateral to four other points gives two constraints per point, x and y, for 8 constraints in total. So a four-point correspondence in general position determines exactly one homography. The user moving the four corner handles is precisely specifying this 8-degree-of-freedom transform.

DLT and Normalization

To find a homography, you set up linear equations from the correspondences. Given a source point `p = (x, y, 1)` and a target point `p' = (u, v, 1)`, you can form the relation `p' × H p = 0`. The cross product being 0 means the two homogeneous coordinate vectors point in the same direction, that is, at the same projective point. Rearranging this gives two linear equations in the entries of H.

With four correspondences, eight such equations pile up. H has nine entries counting scale, but in homogeneous coordinates the overall scale is meaningless, so there are eight degrees of freedom. So you can fix the last entry to 1, or normalize by the norm of the whole vector, to pin down the solution. More generally, you can build a matrix A and solve it as a homogeneous least squares problem of the form `Ah = 0`.

Numerical stability matters here. If the coordinate values are too large, if the four points are bunched on one side, or if they are nearly collinear, the conditioning of the equations gets worse. A small floating-point error can be greatly amplified in the coefficients of H. So I go through Hartley normalization. The centroid of each point set is moved to the origin, and the scale is adjusted so that the average distance is about `sqrt(2)`. Then the homography is computed in normalized coordinates and finally converted back to the original coordinate system.

This normalization looks like simple preprocessing, but it affects result quality quite a lot. Even with the same four points, if the coordinate system is stable the matrix computation wobbles less. In a graphics editor, users can work with a very small layer, work on a very large canvas, or squash the corners nearly into a line. Even in such situations the engine has to produce results that are as predictable as possible.

Internally, there is no need to expose every detailed formula in the UI. The user just grabs and moves four corners. But inside the Rust factory, those four points become linear equations, those equations are stacked into a matrix, that matrix's stability is checked, and the result leads on to pixel sampling. This chain has to hold firm for the perspective tool to feel like a real tool.

Inverse mapping

Getting the perspective transform matrix does not finish the work. You still have to produce the actual image. The important choice here is between forward mapping and inverse mapping. Forward mapping sends each source pixel to a target position. It is intuitive but has a problem: some pixels of the target image may receive no source pixel at all, leaving holes. Holes appear even more easily once scaling or perspective distortion is involved.

So it is more stable to set up rendering as inverse mapping. Walking through the center `(u, v)` of every pixel of the output image, you use the inverse matrix `H^-1` to find which coordinate `(x, y)` of the source image it came from. Because the whole output grid is traversed without gaps, no holes appear. If the source coordinate is outside the image it is treated as a transparent pixel, and if it is inside, you sample.

The source coordinate usually does not land exactly on integer coordinates. You get real-valued coordinates like `(12.37, 45.82)`. If you pick the nearest pixel by nearest neighbor, you get a staircase effect. With bilinear interpolation, you take the weighted average of the four surrounding pixels according to the fractional parts in the x and y directions. If the fractional part in x is tx and in y is ty, the weights of the four pixels are `(1 - tx)(1 - ty)`, `tx(1 - ty)`, `(1 - tx)ty` and `tx ty`.

This interpolation is small math, but it makes a big difference in quality. Especially for images with text or graphics with sharp edges, nearest neighbor looks rough while bilinear looks far more natural. Higher-order interpolation could be considered, but in an editing engine called repeatedly in WASM, the balance between cost and quality matters. For real-time manipulation, bilinear is a pretty good default.

When alpha comes in, things get more delicate. If you interpolate straight RGBA as it is, the RGB of transparent pixels mixes into the result and a fringe appears. So you need to interpolate in premultiplied alpha space and then convert back to straight alpha. In formulas, first send `(r, g, b, a)` to `(ar, ag, ab, a)`, take the weighted sum of the four pixels, and, if a is greater than 0, restore to `(ar/a, ag/a, ab/a, a)`. This small difference makes the edge quality of a perspective transform.

Singular Values and Condition Number

For understanding graphics transforms, a more direct tool than eigenvalues was the singular value decomposition. Any matrix A can be decomposed as `A = UΣVᵀ`. The column vectors of V are orthogonal directions in the input space, the diagonal entries of Σ tell how much each direction is stretched, and U gives the directions the stretched axes point to in the output space. Even a transform that combines rotation, non-uniform scaling and reflection can be read geometrically within this decomposition.

For a 2×2 affine matrix, if the largest singular value is `σ_max` and the smallest is `σ_min`, the condition number is `κ(A) = σ_max / σ_min`. If the two are similar, a circle turns into an ellipse of similar proportions, but if the ratio is very large, one direction stretches a lot while the other is squashed almost into a line. As `σ_min` approaches 0, the inverse transform greatly amplifies input error, and the problem stops being about finding one exact coordinate and becomes about how much noise and rounding get magnified.

Singular values matter from the sampling point of view as well. The footprint a single output pixel occupies in source space is determined by the inverse of the transform. Where there is heavy shrinking in one direction, a wide area of the source must be gathered into one pixel, so a low-pass filter is needed, and where there is enlargement, detail that does not exist must be interpolated. In an anisotropic transform the scales of the two axes differ, so a single global resolution cannot prevent aliasing and blur at the same time.

In a projective transform, this analysis has to be redone at each position. A homography is not globally linear, but near a point it can be linearized with the Jacobian. Taking the singular values of the Jacobian `J(u, v)` at an output position `(u, v)` tells you in which direction and how much that neighborhood is enlarged or compressed relative to the source. In other words, the quality of a perspective transform is not a matter of the scale of a single matrix H but of local conditions distributed over the whole space.

You do not need to compute a fully anisotropic filter at every pixel, but knowing the condition number and the local Jacobian lets you identify numerically dangerous transforms. A nearly collinear quadrilateral, an excessively large magnification and inverse-matrix error are all explained in the same language. Linear algebra became a direct criterion for deciding the boundary where computation loses meaning, while still allowing free geometric manipulation.

Tracing math

Converting pixels to SVG is not detached from linear algebra either. A raster image is samples on a grid, and an SVG path is a curve on the continuous plane. Tracing is the problem of recovering continuous geometry from discrete data. Perfect recovery is impossible. Instead, you find boundaries so that the shape looks the same enough to a person, and approximate those boundaries with suitable paths.

In binary tracing, you first produce grayscale values and decide a threshold. The Otsu threshold finds the cutoff that maximizes the between-class variance when the pixels are split into two classes. Mathematically, it is the problem of finding the t that makes `σ_b²(t) = ω0(t)ω1(t)(μ0(t)-μ1(t))²` large on the histogram. For an image that separates well into black and white, this value gives a good dividing point.

Next you look at connected components and boundaries. On the pixel grid you find regions belonging to the same class and follow the boundary of each region. The boundary here is inherently staircase-shaped. An SVG path does not copy that staircase as it is; it approximates it with straight segments or curve segments. Too precise and the path count gets excessive; too simple and the original shape is ruined. Path precision is, in the end, a trade-off between approximation error and data size.

Color tracing is more complex. You group similar colors in a color space and extract the boundary of each color region separately. A color is a point in the RGB cube, and what counts as similar depends on which metric you use. Colors that look close to a person and colors that are close in RGB Euclidean distance are not always the same. So in a graphics engine, rather than treating all the math in its ideal form, you have to choose what looks stable in real production work.

To make the output SVG convenient as a responsive asset, I also sorted out the handling of viewBox and width/height. The viewBox defines the basis and range of the SVG's internal coordinate system. If you hard-code width and height as fixed pixel values, it can become awkward to use inside EPUB or web documents. So I post-processed the output to keep the geometry's internal coordinate system while leaving outer placement flexible.

Page space

In page output too, coordinate systems kept mattering. A CSS pixel on screen is not a physical length but a logical unit relative to a reference resolution, a device pixel is an actual raster sample, and printed documents use units tied to physical quantities, like points or millimeters. Storing just the number 100 does not tell you whether it is a length on a screen or on a printout. A coordinate must always come with a unit and a reference space.

This problem can be sorted out with dimensional analysis. If a length L is expressed in print points and the resolution is D pixels/inch, the number of raster samples is computed as `N = L × D / 72`. Here L is in points and 72 is points/inch, so the units cancel and only N remains, in pixels. Screen zoom must not enter this physical conversion. Zoom is the observer's magnification, not the size of the document itself.

Contain and cover for placing an image in a page are the same kind of similarity transform. If the original size is `(wi, hi)` and the target box is `(wb, hb)`, the scale for contain is `min(wb/wi, hb/hi)` and for cover is `max(wb/wi, hb/hi)`. Translating by half of the remaining difference gives center alignment. It is a simple formula, but unless you first separate which coordinates are the trim area and which are the actual content area, even the correct ratio gets applied to the wrong box.

Bleed, trim lines and the print marks outside the page are all geometry in the same page space as well. The content box, trim box and bleed box have different containment relationships, and the final page box passed to the output device must hold all of these sets. In a responsive screen the viewport changes the document, but in print the document defines the coordinate system first and the output device samples those coordinates.

Fonts connect to this coordinate system too. Text is not just a string; it is laid out by glyph outlines, advances, kerning and baseline metrics. Even if the same font size is specified on screen and in print, if the actual typeface differs, the line breaks and page count change. For a graphics engine to become a real production tool, not just the math on screen but coordinates, glyphs and physical units had to be preserved with the same meaning inside the file.

The Hidden Factory

What I kept thinking about while building this linear algebra layer was making the math invisible to users. The user does not see the determinant. The user does not see a homography's condition number. The user does not see bilinear weights. But if those values are not computed stably, the user feels something is off right away. The handles shake, the image edges get dirty, and the exported result is subtly misaligned.

So inside the Rust factory I handled coordinate systems, matrices and sampling as strictly as possible. Input with mismatched dimensions, a nearly singular matrix, inverse mapping outside the domain and mixed alpha representations are all different failure conditions. Rather than treating them as one vague image error, I distinguished each as a violation of a separate mathematical contract. The implementation details need not be exposed, but the invariants the internal computation has to keep had to be clear.

This stage was completed as well. Perspective transforms and vectorization, raster sampling and page assembly were organized not as a list of features but as one mathematical processing flow. Linear algebra and projective geometry, singular values and condition numbers, discrete pixels and continuous curves, color vectors and physical units meshed stably inside the graphics factory. In repeated checks, results stayed consistent under extreme coordinates, near-degenerate transforms, various aspect ratios and output resolutions, and the Opkle graphics editor became a tool with a proper math engine behind its UI.

PreviousListNext
CopyrightAll design outputs and videos on this website are copyrighted by Abstract Cloud and protected under copyright law and related regulations. All content, including web, video, text, cover, and interior designs, is the property of the copyright holder. Unauthorized reproduction, distribution, creation of derivative works, or online sharing without prior consent is strictly prohibited. Violations may result in civil and criminal liability under copyright law. Legitimate purchases and copyright protection preserve creators' rights and fuel better works in return.Copyright: Abstract Cloud | CEO: Bae Changkyu
© Abstract Cloud. All Rights Reserved.
HOMEFAQTerms of ServicePrivacy PolicyRefund PolicySeller Informationhelp@opkle.app
+82 10-2747-3403
Business Name :Abstract Cloud  |  CEO :Bae ChangkyuBusiness Registration Number :249-74-00533E-commerce Registration Number :2025-Uijeongbu Songsan-0634Address :402, 108-dong, 49 Buyong-ro, Uijeongbu-si, Gyeonggi-do, Republic of KoreaAll content, designs, and source code on this website
are copyrighted by Opkle.
Opkle