Skip to content

导出

会等待视图内所有网络资源都加载完再进行导出图片。

关键方法

export ( )

export( name: IExportFileType | string, options?: IExportOptions | number | boolean): Promise<IExportResult>

支持导出单个元素、画面截图(App 只能导出画面截图), 默认导出为 1 倍图。

name 为文件名时表示保存文件。

options 为数字时表示图片质量, 为布尔时表示二进制数据 。

ts
type IExportFileType = 'canvas' | 'json' | 'jpg' | 'png' | 'webp' // 后续会支持svg、pdf

interface IExportOptions {
  quality?: number // 设置 jpg / webp 的图片质量
  blob?: boolean // 导出二进制数据

  fill?: string // 设置一个默认背景填充色
  scale?: number // 缩放比例,默认为1,可用于生成小尺寸的缩略图
  pixelRatio?: number // 像素比,默认为1倍图,可导出适配高清屏的2倍图、3倍图...

  slice?: boolean // 是否为切片,将导出切片bounds内的画布上所有内容
  trim?: boolean // 是否裁剪透明像素,默认false
  screenshot?: IBoundsData | boolean // 以当前视图比例导出截图,可指定一个截图区域

  relative?: ILocationType | IUI // 相对坐标系 或父元素 的缩放比例导出,默认Leafer为 inner, 其他元素为 local,可以单独设置: inner |  local | world
  onCanvas?: IExportOnCanvasFunction // onCanvas(canvas => { }) 用于叠加绘制自定义内容
}

interface IExportOnCanvasFunction {
  (canvas: ILeaferCanvas): void
}

interface IExportResult {
  data: ILeaferCanvas | IBlob | string | boolean // data为无时表示导出失败

  width?: number // 图片宽度(实际像素)
  height?: number // 图片高度(实际像素)

  renderBounds?: IBoundsData // 相对父元素的导出bounds,可用于切图定位还原
  trimBounds?: IBoundsData // 裁剪透明像素后的bounds,相对导出bounds,可用于切图定位还原
}

归属

UI

示例

导出 Base64 编码数据

默认图片质量为 0.92。

ts
import { Leafer, Rect } from 'leafer-ui'

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

const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
leafer.add(rect)

rect.export('jpg').then(result => { 
    console.log(result.data)
})

// const result = await rect.export('jpg')

手动设置图片质量。

ts
import { Leafer, Rect } from 'leafer-ui'

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

const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
leafer.add(rect)

rect.export('jpg', 0.5).then(result => { 
    console.log(result.data)
})

// const result = await rect.export('jpg', {quality: 0.5})

导出二进制数据

ts
import { Leafer, Rect } from 'leafer-ui'

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

const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
leafer.add(rect)

rect.export('png', true).then(result => { 
    console.log(result.data)
})

// const result = await rect.export('png', { blob: true })

导出文件

浏览器版会直接下载文件,Node.js 版会保存到指定路径。

ts
import { Leafer, Rect } from 'leafer-ui'

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

const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
leafer.add(rect)

rect.export('test.png') 

// const result = await rect.export('./home/test.png')

导出画布

ts
import { Leafer, Rect } from 'leafer-ui'

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

const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
leafer.add(rect)

rect.export('canvas').then(result => { 
    const leaferCanvas = result.data
    const canvas = leaferCanvas.view
    const context = leaferCanvas.context

    console.log('canvas', canvas) // HTMLCanvasElement
    console.log('2d context', context) // CanvasRenderingContext2D

    // 有和rect.export() 一样的导出函数,导出结果直接是一个data
    canvas.export('a.jpg', 0.9) // options只能设置图像相关的参数
})

// const result = await rect.export('canvas')

导出高清图

ts
import { Leafer, Rect } from 'leafer-ui'

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

const rect = Rect.one({ fill: '#32cd79' }, 100, 100)
leafer.add(rect)

rect.export('HD.png', { pixelRatio: 2 }) 

// const result = await rect.export('HD.png', { pixelRatio: 2 }}

画面截图

ts
import { Leafer, Rect } from 'leafer-ui'

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

leafer.add(Rect.one({ fill: '#32cd79' }, 100, 100))

leafer.export('screenshot.png', { screenshot: true }) 

// const result = await leafer.export('screenshot.png', {screenshot: true}

Released under the MIT License.