Overview: Understanding Targets
Targets are the building blocks of TargetJS. They provide a unified way to define values and methods, with each target maintaining its own execution state and lifecycle.
A target can execute a method, animate a value over time, fetch data, respond to events, create children, repeat operations, introduce delays between executions, or invoke callbacks when its task completes.
Code-Order Execution
In TargetJS, the order in which targets are written determines their execution sequence. There is no need to explicitly call methods. Targets can automatically defer their execution until preceding targets have completed their tasks, including waiting for any children created by those targets to finish their own operations.
Think of target execution as an assembly line. Each target represents a stage in the process, with execution progressing from one stage to the next. A stage can branch into multiple parallel execution lines by creating children, each with its own sequence of targets. Once those branches complete their tasks, they converge back into the main execution line, allowing the next stage to proceed.
This approach makes it possible to express complex asynchronous workflows through simple, declarative code, without callbacks and promises.
A Simple Animated Box
This example illustrates a single assembly line. First, it animates the width of a box from 100 to 250 pixels and back to 100.
It then waits for one second before scaling the box to three times its original size and back.
Targets with CSS property names, such as backgroundColor, height, and width
directly control the corresponding styles of the element.
How It Works in Detail
-
backgroundsets the box's color tomediumpurple. -
widthanimates through[100, 250, 100]using 20 steps and a 10-millisecond interval. -
letsWait$$insert a pause of second after thewidthis done. Double$$suffix tells TargetJS to wait for the preceding target's execution chain to finish. -
scale$$scales the box when the all preceding ends their execution.
The Same Example in HTML
TargetJS also supports declarative HTML using tg- attributes.
The same animation sequence can be expressed directly in HTML without defining a JavaScript object:
<div
tg-backgroundcolor="mediumpurple"
tg-height="100"
tg-width="{ value: [100, 250, 100], steps: 20, interval: 10 }"
tg-letswait$$="{ interval: 1000}"
tg-scale$$="{ value: [1, 3, 1], steps: 20, interval: 10 }"
></div>
Coordinating Parallel Assembly Lines
This example illustrates a more complex assembly line. First, we create 10 boxes that animate their widths.
Once all 10 boxes have completed their animations, we introduce a one-second pause before scaling the entire container.
For better performance, the 10 boxes are rendered using WebGPU. The default renderer is dom.