Skip to content

Setting Styles

Initial Style

Create a rectangle with a dashed border style.

ts
// #初始化元素样式
import { Leafer, Rect } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    x: 100,
    y: 100,
    width: 100,
    height: 100,
    stroke: '#32cd79',
    strokeWidth: 2,
    dashPattern: [6, 6] // 绘制虚线
})

leafer.add(rect)

Modify Style

ts
// 标准修改
rect.stroke = 'blue'
rect.strokeWidth = 4

Simple Update

ts
// 简洁修改
rect.set({
    stroke: 'blue',
    strokeWidth: 4,
})

Reset Style

ts
// 重置样式
rect.reset()

// 重置为新样式
rect.reset({
    stroke: 'blue',
    strokeWidth: 4,
})

Modifying Objects

Note

Elements can only detect first-level property setter changes. Directly modifying rect.fill.url = url will not trigger a re-render.

ts
// #修改样式 [修改对象]
import { Leafer, Rect } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    fill: {
        type: 'image',
        url: '/image/leafer.jpg',
    }
})

leafer.add(rect)

// 修改对象
setTimeout(() => {
    // 元素只能检测到 第一层级属性 的 setter 变化
    // 需要给 fill 再次赋值, 单独修改 rect.fill.url = url 不会渲染更新
    rect.fill = {
        type: 'image',
        url: '/image/arrows.png',
    }
}, 1000)

Using JSON

Learn about JSON data import and export.

ts
// #修改数据 [使用 JSON (Leafer)]
import { Group, Leafer } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const group = new Group()

leafer.add(group)

const json = { "x": 20, "y": 20, "children": [{ "tag": "Rect", "x": 100, "y": 100, "width": 200, "height": 200, "fill": "#32cd79", "draggable": true }] }

group.set(json)

Introduction to Element Appearance Styles

fill

Fill, similar to background-color in HTML5 or color for text.

Supports solid color, linear gradient, radial gradient, angular gradient, and image fill. Multiple fills can be stacked.

ts
// #线性渐变填充 [默认方向 (Leafer)]
import { Leafer, Rect } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    width: 100,
    height: 100,
    fill: {
        type: 'linear', // 从顶部居中 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027']
    },
})

leafer.add(rect)

stroke

Stroke, similar to border-color in HTML5.

Supports solid color, linear gradient, radial gradient, angular gradient, and image pattern. Multiple strokes and sub-stroke styles are supported.

ts
// #线性渐变描边 [默认方向 (Leafer)]
import { Leafer, Rect } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    width: 100,
    height: 100,
    stroke: {
        type: 'linear',  // 从顶部居中 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027']
    },
})

leafer.add(rect)

Further stroke styling options are supported:

NameDescriptionValue
strokeAlignStroke alignment'inside', 'center', 'outside'
strokeWidthStroke widthnumber
strokeScaleFixedWhether stroke width is fixed (not affected by zoom)'zoom-in', number, boolean
strokeCapStroke line cap style'none', 'round', 'square'
strokeJoinStroke corner style'miter', 'bevel', 'round'
dashPatternDashed stroke patternnumber[]
dashOffsetDash offsetnumber
ts
// #描边样式
import { Leafer, Line } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Line({
    y: 100,
    stroke: '#32cd79',
    strokeWidth: 5, // 描边的宽度
    strokeScaleFixed: true, // 是否固定线宽(不受视图放大影响)
    strokeAlign: 'center', // 描边的对齐方式 'inside'、'center' 、'outside'
    strokeCap: 'round', // 描边的端点形状 'none' 、'round' 、'square'
    strokeJoin: 'round', // 描边的拐角处理 'miter' 、'bevel' 、'round'
    dashPattern: [15, 15], // 绘制虚线
    dashOffset: 15,	// 虚线描边的起点偏移值
})

leafer.add(rect)

shadow

Outer shadow of an element, supports multiple stacked shadows and box-shadow-like effects.

ts
// #外阴影 [drop-shadow (Leafer)]
import { Leafer, Rect } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    width: 100,
    height: 100,
    cornerRadius: 30,
    fill: 'rgba(50,205,121,0.7)',
    shadow: {
        x: 10,
        y: -10,
        blur: 20,
        color: '#FF0000AA'
    }
})

leafer.add(rect)

innerShadow

Inner shadow of an element, supports multiple inner shadows.

ts
// #内阴影 (Leafer)
import { Leafer, Rect } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    width: 100,
    height: 100,
    cornerRadius: 30,
    fill: '#32cd79',
    innerShadow: {
        x: 10,
        y: 5,
        blur: 20,
        color: '#FF0000AA'
    }
})

leafer.add(rect)

mask

Masking feature: assign an element inside a Group as a mask to create complex clipping effects. Supports 5 mask types.

ts
// #遮罩功能 [将圆环设为遮罩 (Leafer)]
import { Leafer, Group, Ellipse, Image } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const group = new Group({ x: 100, y: 100 })

const mask = new Ellipse({
    width: 100,
    height: 100,
    innerRadius: 0.5,
    fill: 'black',
    mask: true
})

const image = new Image({
    width: 100,
    height: 100,
    url: '/image/leafer.jpg'
})

leafer.add(group)

group.add([mask, image])   

eraser

Eraser feature: assign an element inside a Group as an eraser to create complex erase effects. Supports 2 eraser types.

ts
// #擦除功能 [将圆环设为橡皮擦 (Leafer)]
import { Leafer, Group, Ellipse, Image } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const group = new Group({ x: 100, y: 100, draggable: true })

const image = new Image({
    width: 100,
    height: 100,
    cornerRadius: 30,
    url: '/image/leafer.jpg'
})

const eraser = new Ellipse({
    x: 15,
    y: 15,
    width: 70,
    height: 70,
    innerRadius: 0.5,
    fill: 'black',
    eraser: true,
    draggable: true
})

leafer.add(group)

group.add([image, eraser])  

Element Visibility

visible

Element visibility, used to hide elements.

ts
// 隐藏元素
rect.visible = false

opacity

Element opacity.

ts
// 设置不透明度
rect.opacity = 0.5

Advanced Positioning Properties

origin

Rotate and scale elements around the origin point, similar to CSS transform-origin.

ts
// #原点 [围绕原点旋转 45 度 (Leafer)]
import { Leafer, Rect, Frame } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    x: 25,
    y: 25,
    width: 50,
    height: 50,
    origin: 'center',
    rotation: 45,
    draggable: true,
    fill: '#4DCB71'
})

leafer.add(new Frame({ width: 100, height: 100, fill: '#FF4A2C', children: [rect] }))

around

Draw elements around the around point, similar to the anchor system in game engines.

Difference from origin: it adds an extra step that moves the element's internal around point to the element's (x, y) coordinate.

Draw around center point

ts
// #around 属性 [围绕坐标(50,50) 为中心旋转 45 度 (Leafer)]
import { Leafer, Rect, Frame } from 'leafer-ui'

const leafer = new Leafer({ view: window })

const rect = new Rect({
    x: 50,
    y: 50,
    width: 50,
    height: 50,
    around: 'center',
    rotation: 45,
    fill: '#4DCB71',
    draggable: true
})

leafer.add(new Frame({ width: 100, height: 100, fill: '#FF4A2C', children: [rect] }))

Overview of Common Element Properties

NameDescription
Identifier
idUnique id of the element
tagElement tag name (i.e. class name of the element)
nameElement name
classNameClass name, currently supports only 1 value
innerIdRuntime generated temporary id (incremental), cannot be used for remote storage
innerNameRuntime generated temporary name, used for quick element identification
Layout
xX-axis position
yY-axis position
widthWidth
heightHeight
isAutoWidthWhether it is auto width
isAutoHeightWhether it is auto height
lockRatioWhether to lock aspect ratio of the element, requires resize plugin
scaleXX-axis scale factor, negative value means mirroring on X axis
scaleYY-axis scale factor, negative value means mirroring on Y axis
scaleFixedFix global scale of the element, not affected by viewport zoom
scaleQuick set / get scaleX, scaleY
rotationRotation angle
skewXX-axis skew angle
skewYY-axis skew angle
offsetXX-axis offset
offsetYY-axis offset
originRotate/scale around origin, same as CSS transform-origin
aroundDraw element around a point, similar to anchor point in game engines
Bounding Box
boxBoundsBase bounds in internal coordinate system (OBB)
renderBoundsRender bounds in internal coordinate system (AABB)
worldBoxBoundsBase bounds in world coordinate system (AABB)
worldRenderBoundsRender bounds in world coordinate system (AABB)
renderSpreadForce expand render bounds to avoid incorrect text measurement rendering issues
Transform
worldTransformTransform matrix in world coordinate system, includes scaleX/scaleY
localTransformTransform matrix relative to parent
Appearance
zIndexStack order within parent
opacityOpacity
worldOpacityGlobal opacity (affected by parent hierarchy)
visibleVisibility of element
fillFill, similar to HTML background-color or text color. Supports solid, linear gradient, radial gradient, angular gradient, image pattern, etc. Multiple fills can be stacked
strokeStroke, similar to HTML border-color. Supports multiple paint types and stacking
strokeAlignStroke alignment
strokeWidthStroke width
strokeScaleFixedWhether stroke width is fixed (not affected by zoom)
strokeCapStroke line cap
strokeJoinStroke line join
dashPatternDashed stroke pattern
dashOffsetDash offset
shadowOuter shadow, supports multiple shadows (box-shadow like)
innerShadowInner shadow, supports multiple shadows
dimDim element, used with bright, requires highlight plugin
dimskipSkip dimming and highlight element, used with dim, requires highlight plugin
brightHighlight element, rendered on top layer, used with dim, requires highlight plugin
maskSet as mask
eraserSet as eraser
blendModeBlend mode
Interaction
hittableWhether element responds to pointer/touch events (like CSS pointer-events)
hitChildrenControl interactivity of children
hitSelfControl interactivity of self
hitFillHit detection for fill (pixel-level detection supported for PNG/SVG)
hitStrokeHit detection for stroke
editableWhether element is editable, requires editor plugin
draggableWhether element is draggable
dragBoundsLimit drag range
dragBoundsTypeDrag bounds type (inside or outside)
cursorCursor style on hover (CSS cursor values supported)
State
statesPredefined states for switching element/game states, supports transitions
stateCurrent state
buttonSet as button, syncs child interaction states
hoverStyleHover style
pressStylePress style
focusStyleFocus style
selectedWhether selected
selectedStyleSelected style
disabledWhether disabled
disabledStyleDisabled style
Animation
animationAnimation property (timing, loop, keyframes, motion path)
transitionState transition animation
transitionOutExit transition animation
motionPathSet motion path
motionPrecisionMotion path precision
motionPosition on motion path
motionRotationRotation offset along motion path
Relationship
parentParent element
leaferLeafer engine instance
appApp instance (or Leafer in non-App mode)
isLeaferWhether it is Leafer engine
leaferIsCreatedEngine and children created
leaferIsReadyEngine ready after first layout
zoomLayerZoom/pan layer
Data
dataUser-defined data object
__Internal data instance
proxyDataReactive support for Vue/React
pathPath data supporting SVG/Canvas commands

Overview of Common Element Methods

NameDescription
Layout
move()Move element (incremental), supports optional animation transition
moveInner()Move in inner coordinate system (incremental), supports animation
moveWorld()Move in world coordinate system (incremental), supports animation
resizeWidth()Resize width (requires resize plugin)
resizeHeight()Resize height (requires resize plugin)
scaleResize()Convert scale to width/height (incremental)
scaleOf()Scale around origin (box coords), supports animation
scaleOfWorld()Scale around world origin (incremental)
flip()Flip/mirror element in world coordinates
rotateOf()Rotate around origin (box coords)
rotateOfWorld()Rotate around world origin
skewOf()Skew around origin (box coords)
skewOfWorld()Skew around world origin
Bounding Box
getBounds()Get AABB bounds
getLayoutBounds()Get OBB layout bounds
getLayoutPoints()Get layout corner points
Transform
setTransform()Set transform matrix
getTransform()Get transform matrix
transform()Apply transform (incremental)
transformWorld()World transform
Coordinate Conversion
getPagePoint()World → page
getLocalPoint()World → local
getInnerPoint()World → inner
getBoxPoint()World → box
getWorldPointByPage()Page → world
getWorldPointByLocal()Local → world
getInnerPointByLocal()Local → inner
getWorldPoint()Inner → world
getLocalPointByInner()Inner → local
getBoxPointByInner()Inner → box
getWorldPointByBox()Box → world
getInnerPointByBox()Box → inner
Actions
dropTo()Drag element to container
focus()Focus element
Animation & Motion
animate()Run animation
getMotionTotal()Get motion path length
getMotionPoint()Get motion point
Events
on()Listen events
on_()Listen with bind
once()Listen once
off()Remove listener
off_()Remove bound listener
emit()Emit event
emitEvent()Emit event object
Update
forceUpdate()Force update
updateLayout()Update layout
forceRender()Force render
nextRender()Next frame callback
removeNextRender()Remove callback
Search
find()Find elements
findTag()Find by tag
findOne()Find single element
findId()Find by id
pick()Pick by coordinate
Data
set()Set data
reset()Reset data
get()Get data
setAttr()Set attribute
getAttr()Get attribute
getComputedAttr()Get computed attribute
clone()Clone element
Path
getPath()Get path
getPathString()Get path string
Export
export()Export element
syncExport()Sync export
toJSON()Export JSON
toString()Export string
Remove
remove()Remove element
destroy()Destroy element

Next Step

Event Handling

Released under the MIT License.