Removing Elements
Standard Removal
ts
Conditional Removal
Same parameters as the find() method. It first performs find() internally and then removes all matched elements in batch.
Note
You need to install the find element plugin to use this feature, or install leafer-game or leafer-editor (both already include the find element plugin).
ts
// #移除元素 [条件移除]
import { Leafer, Rect } from 'leafer-ui'
import '@leafer-in/find' // 导入查找元素插件
const leafer = new Leafer({ view: window })
const rect = Rect.one({ id: 'book', fill: '#32cd79' }, 100, 100)
const rect2 = Rect.one({ fill: 'blue' }, 300, 100)
leafer.addMany(rect, rect2)
setTimeout(() => {
// 移除 id 为 book 的元素
leafer.remove('#book') // [!code hl] // 等同于 leafer.find('#book').forEach(item => item.remove())
}, 2000)Destroy Removal
ts
Clearing Elements
ts
// #移除元素 [清空元素]
import { Leafer, Rect } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
const rect2 = Rect.one({ fill: 'blue' }, 300, 100)
leafer.addMany(rect, rect2)
setTimeout(() => {
leafer.clear() // [!code hl] // 清空并销毁所有子元素
}, 2000)Destroy Engine
ts