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案例,放在目录里面。

相关推荐
醉城夜风~25 分钟前
CSS元素显示模式(display)
前端·css
AI大模型-小华1 小时前
Codex 三方充值快速入门指南
java·前端·数据库·chatgpt·ai编程·codex·chatgpt pro
做前端的娜娜子3 小时前
同一链接实现 PC Web 与移动 H5 自适应
前端·掘金·金石计划
小帅不太帅4 小时前
架构没变、规模没变,DeepSeek V4 Flash 正式版凭什么暴涨 47 分?
前端·aigc·deepseek
jarvisuni4 小时前
DeepSeekFlash前端依旧拉垮,而且变慢了很多!
前端·javascript·算法
卷福同学5 小时前
AI编程出海第二步:验证关键词能否做站
前端·人工智能·后端
赵庆明老师6 小时前
Vben精讲:21-详解web-antd:tsconfig.json
前端·json·vim
wc886 小时前
微软EDGE浏览器功能学习
前端·学习·edge
破z晓6 小时前
javascript 导出excel表
开发语言·javascript·excel