温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用纯JS实现轻量化图片编辑器

发布时间:2022-10-22 09:55:12 来源:亿速云 阅读:143 作者:iii 栏目:web开发

本篇内容主要讲解“怎么用纯JS实现轻量化图片编辑器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用纯JS实现轻量化图片编辑器”吧!

    Optimizer 框架特点

    怎么用纯JS实现轻量化图片编辑器

    • 事件、画图、交互 全局管理

    • 支持注册自定义组件, 可自定义配置管理

    • 基于面向对象, 高度抽象代码

    • 简单易用, 能快速开发出各种效果

    Optimizer 框架使用

    启动

    首先需要场景管理器, 通过继承 GenScene 来创建场景, 场景里对于页面中的多个控制器进行管理

    class MainScene extends GenScene {
        constructor(optimizer) {
            super(optimizer)
        }
    }

    全局使用 instance 获取实例, 加载场景管理器, 最简单的 Optimizer 程序就启动了

    GenOptimizer.instance(function(o){
        let scene = MainScene.new(o)
        o.runWithScene(scene)
    })

    场景管理器 (Scene)

    页面事件Event

    ...
    <div class='gen-auto-button-area'>
        <button class='gen-auto-button' data-value='config.arg1'>text</button>
    </div>
    ...
    // 注册页面 class, 全局可用
    this.registerPageClass({
        "buttonArea": 'gen-auto-button-area',
        ...
    })
    // 注册全局事件       
    this.registerGlobalEvents([     
        {
            eventName: "click",
            // 事件绑定的元素区域
            className: sc.pageClass.buttonArea,
            // 在 所有 configToEvents 响应之 前 触发
            after: function(bindVar, target) {
                // bindVar: 绑定的变量
                // target: 事件触发的目标
            },        
            // 在 所有 configToEvents 响应之 后 触发
            before: function(bindVar, target) {
                // bindVar: 绑定的变量
                // target: 事件触发的目标
            },
            // 事件响应
            configToEvents: {
                // 自定义绑定的变量: 事件触发后的响应
                "config.arg1": function(target) {
                },
                "action.arg1": function(target) {
                },
                ...
            }
        },
        ...
    ])

    鼠标事件

    this.resgisterMouse(function(event, action) { 
        // event 是鼠标点击的事件
        // action 为鼠标点击的事件名称    
        if (action == 'mouseleave') {
            console.log('mouseleave canvas')
        } else if (action == 'up') {
            console.log('up canvas')
        } else if (action == 'down') {
            console.log('down canvas')
        } else if (action == 'move') {
            console.log('move canvas')
        }
    })

    键盘事件

    this.registerAction("Backspace", status => {
        // status 为 'down' 时, 表示按下, 为 'up' 时, 表示松开
        console.log("Backspace", status)
    })
    this.registerAction("s", status => {
        // status 为 'down' 时, 表示按下, 为 'up' 时, 表示松开
        console.log("s", status)
    })

    注册组件 Component

    class MyComponent extends GenComponent {
        constructor(control) {
            super(control.scene)
            this.control = control
        }
        ...
    }
    this.bindComponent('attribute', MyComponent.new(this))

    使用组件

    // 全局可使用组件
    let data = ...
    this.getComponent('attribute').buildWith(data)

    到此,相信大家对“怎么用纯JS实现轻量化图片编辑器”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    js
    AI