Bipes项目二次开发/海龟编程(六)

Bipes项目二次开发/海龟编程(六)

海龟编程,之前是有出过一期blog.csdn.net/weixin_4485... ,这次我将这个功能加到项目里面,前端python还是用skulpt.js。接下看我操作。

第一:配置

配置加了新模式,海龟编程。选择海龟编程后,运行按钮,将会运行海龟画图功能。 skulpt-controller代码改动

cpp 复制代码
// 新增画图输出
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'draw-canvas'
window.Sk.TurtleGraphics.width = 600
window.Sk.TurtleGraphics.height = 600

// 新增运行海龟函数
runSkulptCode() {
        let code = Code.generateCode()
        let myPromise = this.sk.misceval.asyncToPromise(() => {
            return this.sk.importMainWithBody("<stdin>", false, code, true);
        })

        myPromise
            .then((res) => {})
            .catch((err) => {})
}

第二:静态页面

cpp 复制代码
<div class="draw-preview">
    <div class="draw-header">
      <span class="draw-header-left">海龟图</span>
      <div class="header-right">
        <img class="draw-close" src="./media/new-icon/close.png" title="关闭海龟">
      </div>
    </div>
    <div class="draw-content">
      <div id="draw-canvas" style="width: 600px; height: 600px;"></div>
    </div>
</div>

第三:js引用

cpp 复制代码
import Common from "./common";
import EventEmitterController from '../utils/event-emitter-controller'
import DragController from '../utils/drag-controller'

export default class DrawPreview extends Common {
    constructor() {
        super()
        // 创建拖拽容器
        this.element = new DragController(
            document.getElementsByClassName('draw-preview')[0],
            {
                minWidth: 400,
                minHeight: 400,
                maxWidth: 600,
                maxHeight: 600
            },
            'draw',
            (params) => {
                let { newWidth, newHeight } = params
                $('#draw-canvas').css({
                    width: newWidth + 'px',
                    height: newHeight - 52 + 'px'
                })
                window.Sk.TurtleGraphics.width = newWidth
                window.Sk.TurtleGraphics.height = newHeight - 52
                // console.log(params)
            }
        )

        this.state = false

        EventEmitterController.on('draw-preview-change', (state) => {
            this.changeCode(state)
        })
    }
    initEvent() {
        $('.draw-close').on('click', () => {
            this.changeCode(false)
        })
    }
    // 显示隐藏预览区
    changeCode(state) {
        $('.draw-preview').css('visibility', (state ? 'visible' : 'hidden'))
        this.state = state
    }
}

第四:效果

总结

这期的改动的代码也不多,不过现在项目会存在功能相互矛盾的地方,后期打算规划一下,让对应模式下,显示对应的功能模块。海龟编程其实也是挺有意思的,后期也打算出一些demo案例,放在目录里面。

相关推荐
夏幻灵8 小时前
HTML5里最常用的十大标签
前端·html·html5
冰暮流星8 小时前
javascript之二重循环练习
开发语言·javascript·数据库
Mr Xu_8 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝8 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions8 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发8 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_8 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞058 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、8 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao8 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架