Skip to content

GradientEditor 类

渐变编辑工具,支持调用 editor.openInnerEditor() 进入渐变编辑模式。

继承

GradientEditor  >  InnerEditor

📆 更新日志

当前为 v1.0.0,查看更新日志

📦 安装插件(本地安装)

本插件不发布于公开 NPM 仓库,通过本地 .tgz 文件安装使用,需 购买插件 授权后才能使用。

第一步:获取插件包

购买后,你将获得一个名为 pxgrow-gradient-editor-1.0.0.tgz 的安装包。

将该文件放置在你的项目根目录下的 pxgrow 文件夹中统一管理,安装后请勿删除。

第二步:本地安装命令

根据你使用的包管理器,选择以下方式之一:

sh
npm install ./pxgrow/pxgrow-gradient-editor-1.0.0.tgz
sh
pnpm add ./pxgrow/pxgrow-gradient-editor-1.0.0.tgz
sh
yarn add ./pxgrow/pxgrow-gradient-editor-1.0.0.tgz
sh
bun add ./pxgrow/pxgrow-gradient-editor-1.0.0.tgz

将在 package.json 中自动增加本地依赖:

"@pxgrow/gradient-editor": "file:pxgrow/pxgrow-gradient-editor-1.0.0.tgz"


或通过 script 标签引入,使用全局变量 PxGrow.gradientEditor 访问插件内部功能。

需解压 pxgrow-gradient-editor-1.0.0.tgz 文件,复制 package/dist/gradient-editor.js 使用。

html
<script src="/lib/pxgrow/gradient-editor.js"></script>
<script>
  const { GradientEditor } = PxGrow.gradientEditor
</script>

关键属性

editTarget: UI

当前编辑的目标元素。

gradientTransformTool: TransformTool

渐变框的变换操作工具,支持手动移动、旋转、调整大小。

显示属性

gradientEditBox: EditBox

渐变编辑框实例。

配置属性(只读)

userConfig: IGradientEditorConfig

用户的配置。

mergeConfig: IGradientEditorConfig

实际使用的编辑器配置,实时合并编辑器的默认 config 与 userConfig,频繁访问会有性能开销。

mergedConfig: IGradientEditorConfig

mergeConfig 的缓存,频繁访问不会有性能问题。

关键方法

getOffsetColor ( offset: number ): string

获取指定位置的颜色,offset取值范围 0~1。

addColorStop ( offset: number, color?: IColor)

添加一个渐变色到指定位置, offset取值范围 0~1。

selectColorStop ( index: number)

选中一个渐变色,index为渐变色的索引位置。

sortColorStops ( )

排序渐变色。

deleteColorStop( index?: number)

删除当前选中的渐变色,可指定需要删除的渐变色index索引。

更新

updateEditor ( )

更新渐变编辑器,会卸载、再加载一次。

update ( )

更新显示渐变框和图片的显示。

updatePaint( updateColorStops?: boolean )

更新渐变paint对象, updateColorStops 表示是否更新渐变色。

GradientEditor 配置

基础     高级

示例

线性渐变

ts
// #渐变编辑 [线性渐变]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/animate' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)


import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {}
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器
    fill: {
        type: 'linear', // 从顶部居中 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027', '#79CB4D', '#FF4B4B']
    }
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

径向渐变

ts
// #渐变编辑 [径向渐变]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)

import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {}
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器
    fill: {
        type: 'radial', // 从中心 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027', '#FF4B4B']
    }
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

角度渐变

ts
// #渐变编辑 [角度渐变]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)

import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {}
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器
    fill: {
        type: 'angular', // 从中心 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027', '#79CB4D', '#FF4B4B']
    }
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

默认选中一个色标

ts
// #渐变编辑 [默认选中一个色标]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)


import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {}
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器 
    fill: {
        type: 'linear',
        stops: [
            '#FF4B4B',
            '#FEB027',
            { offset: 0.66, color: '#79CB4D', selected: true }, // 默认选中绿色
            '#FF4B4B'
        ]
    }
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

在多个 fill、stroke 中指定需要编辑的渐变对象

ts
// #渐变编辑 [在多个 fill、stroke 中指定需要编辑的渐变对象]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)


import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {}
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    strokeWidth: 10,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器 
    fill: {
        type: 'radial',
        stops: ['#FF4B4B', '#FEB027', '#FF4B4B'],

    },
    stroke: {
        type: 'linear',
        stops: ['#FF4B4B', '#FEB027', '#79CB4D', '#FF4B4B'],
        editing: true, // 指定stroke为需要编辑渐变的对象
    },
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

配置色块样式

ts
// #渐变编辑 [配置色块样式]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/animate' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)


import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {
        GradientEditor: {
            colorBox: { // 配置色块样式
                cornerRadius: 0,
                selectedStyle: { y: -4, scaleX: 1.15, scaleY: 1.15, stroke: '#0d99ff' } // 选中样式
            },
            colorArrow: { // 配置色块箭头样式
                selectedStyle: { fill: '#0d99ff' } // 选中样式
            }
        }
    }
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器
    fill: {
        type: 'linear', // 从顶部居中 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027', '#79CB4D', '#FF4B4B']
    }
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

配置渐变编辑框的控制点和线

ts
// #渐变编辑 [配置渐变编辑框的控制点和线]
import { App, Rect } from 'leafer-ui'
import '@leafer-in/editor' // 导入图形编辑器插件
import '@leafer-in/state' // 导入状态插件
import '@leafer-in/animate' // 导入状态插件
import '@leafer-in/color' // 导入颜色插件 
import '@leafer-in/viewport' // 导入视口插件 (可选)


import '@pxgrow/gradient-editor' // 导入渐变编辑插件

const app = new App({
    view: window, editor: {
        GradientEditor: {
            gradientEditBox: { // 配置渐变编辑框
                pointSize: 14,
                point: { fill: '#0d99ff', strokeWidth: 0, shadow: { x: 0, y: 0, blur: 5, color: 'rgba(0,0,0,0.3)' } }, // 控制点
                rect: { stroke: '#0d99ff', shadow: { x: 0, y: 0, blur: 5, color: 'rgba(0,0,0,0.2)' } }, // 控制线
            },
            colorBoxOffset: 6 // 颜色框偏移
        }
    }
})

const rect = new Rect({
    x: 100,
    y: 100,
    width: 200,
    height: 200,
    editable: true,
    editInner: 'GradientEditor', // 指定内部编辑器
    fill: {
        type: 'linear', // 从顶部居中 -> 底部居中垂直绘制的渐变
        stops: ['#FF4B4B', '#FEB027', '#79CB4D', '#FF4B4B']
    }
})

app.tree.add(rect)

// 模拟双击元素打开渐变编辑器
setTimeout(() => {
    app.editor.openInnerEditor(rect, true)
}, 600)

Released under the Commercial License Agreement.