静雅斋 成就更优秀的你

Desmos 课件从 Iframe 嵌入到 API 嵌入转化 | 课件模板

前情概要

在 Desmos 服务器端做好的课件,可以用 Iframe 框架将以下两种不同形式的链接来嵌入网页:

① https://www.desmos.com/calculator/j3lhhpak2h?lang=zh-CN ,这个链接格式的优点是有左侧栏的表达式列表,且是展开的,能知道课件的如何做出来的,缺点是有个黑边,不美观;

② https://www.desmos.com/calculator/j3lhhpak2h?embed&lang=zh-CN ,这个链接格式的优点没有黑边美观干净,缺点是没有左侧的表达式列表,不知道这个课件是用了什么内容做出来的;

那么,有没有能自由定义的形式,兼容以上的优点,规避缺点的做法呢,回答是肯定的,就是要用到 API 。本文就是记录如何将做好的课件转化为用 API 来嵌入。

实现步骤

1.浏览器访问:https://www.desmos.com/calculator/j3lhhpak2h;

2.按F12切换到 Console 控制台;

3.输入:JSON.stringify(Calc.getState()),然后回车;

4.复制输出的全部文本,格式如下,其实就是一串 JSON 文本,要让我们自己编辑是非常困难的;

'{"version":11,"randomSeed":"5ade43f9274e2be2af442031debd5331","graph":{"viewport":{"xmin":-7.910255341689902,"ymin":-5.6808249016011585,"xmax":14.667869658310105,"ymax":6.12992964501225},"showGrid":false,"xAxisArrowMode":"POSITIVE","yAxisArrowMode":"POSITIVE","__v12ViewportLatexStash":{"xmin":"-7.910255341689902","xmax":"14.667869658310105","ymin":"-5.6808249016011585","ymax":"6.12992964501225"}},"expressions":{"list":[{"type":"expression","id":"3","color":"#388c46","latex":"s=1","slider":{"hardMin":true,"hardMax":true,"min":"0","max":"1"}},{"type":"expression","id":"6","color":"#c74440","latex":"f\\\\left(x\\\\right)=2^{x}\\\\left\\\\{-5\\\\le x\\\\le5\\\\right\\\\}"},{"type":"expression","id":"9","color":"#6042a6","latex":"x_{0}=\\\\left[-5,-4.5,...,5\\\\right]"},{"type":"expression","id":"12","color":"#388c46","latex":"A=\\\\left((1-s)\\\\cdot x_{0}+s\\\\cdot f(x_{0}),\\\\ (1-s)\\\\cdot f(x_{0})+s\\\\cdot x_{0}\\\\right)","lines":true,"description":"ghadfg","clickableInfo":{"enabled":true}},{"type":"expression","id":"15","color":"#fa7e19","latex":"x=2^{y}\\\\left\\\\{-5\\\\le y\\\\le5\\\\right\\\\}"},{"type":"expression","id":"16","color":"#c74440","latex":"\\\\left(s+3,-3\\\\right)","showLabel":true,"label":"拖动,试试!"},{"type":"expression","id":"17","color":"#6042a6","latex":"y=x\\\\left\\\\{-4\\\\le x\\\\le8\\\\right\\\\}","lineStyle":"DASHED","lineWidth":"1"}]},"includeFunctionParametersInRandomSeed":true,"doNotMigrateMovablePointStyle":true,"backmigrated":14}'

5.将上述代码块两端的单引号 ' 都去掉,同时在最后的右大括号后边添加 ;,然后复制粘贴到实例代码中,替换 graphState 变量内容;

6.其他地方的代码都不动,保存退出即可。

实例代码

<style>
#calculator{width:90%;aspect-ratio:16/9;position:relative;margin:0 auto;}
.dcg-explore-link,.dcg-watermark,.dcg-trial-notice,[class*="trial"],[class*="notice"],[class*="powered"],[class*="desmos"]{display:none !important;}
.dcg-branding{display:none !important;}
#fsbtn{position:absolute;right:8px;bottom:8px;padding:4px 10px;cursor:pointer;z-index:999;background:#216ede;color:#fff;border:none;border-radius:4px;}
</style>
<div id="calculator">
<button id="fsbtn" onclick="toggleFullscreen()">⛶ 全屏</button>
</div>
<script>
const script=document.createElement('script');
script.src="https://www.desmos.com/api/v1.12/calculator.js?apiKey=c8b9bc90feef4b85be805671035a429c&lang=zh-CN";
script.onload=()=>{
const elt=document.getElementById('calculator');
const calc=Desmos.GraphingCalculator(elt,{expressions:true,expressionsCollapsed:true,settingsMenu:true});
calc.updateSettings({language:'zh-CN'});
const graphState={"version":11,"randomSeed":"5ade43f9274e2be2af442031debd5331","graph":{"viewport":{"xmin":-7.910255341689902,"ymin":-5.6808249016011585,"xmax":14.667869658310105,"ymax":6.12992964501225},"showGrid":false,"xAxisArrowMode":"POSITIVE","yAxisArrowMode":"POSITIVE","__v12ViewportLatexStash":{"xmin":"-7.910255341689902","xmax":"14.667869658310105","ymin":"-5.6808249016011585","ymax":"6.12992964501225"}},"expressions":{"list":[{"type":"expression","id":"3","color":"#388c46","latex":"s=1","slider":{"hardMin":true,"hardMax":true,"min":"0","max":"1"}},{"type":"expression","id":"6","color":"#c74440","latex":"f\\left(x\\right)=2^{x}\\left\\{-5\\le x\\le5\\right\\}"},{"type":"expression","id":"9","color":"#6042a6","latex":"x_{0}=\\left[-5,-4.5,...,5\\right]"},{"type":"expression","id":"12","color":"#388c46","latex":"A=\\left((1-s)\\cdot x_{0}+s\\cdot f(x_{0}),\\ (1-s)\\cdot f(x_{0})+s\\cdot x_{0}\\right)","lines":true,"description":"ghadfg","clickableInfo":{"enabled":true}},{"type":"expression","id":"15","color":"#fa7e19","latex":"x=2^{y}\\left\\{-5\\le y\\le5\\right\\}"},{"type":"expression","id":"16","color":"#c74440","latex":"\\left(s+3,-3\\right)","showLabel":true,"label":"左右拖动!"},{"type":"expression","id":"17","color":"#6042a6","latex":"y=x\\left\\{-4\\le x\\le8\\right\\}","lineStyle":"DASHED","lineWidth":"1"}]},"includeFunctionParametersInRandomSeed":true,"doNotMigrateMovablePointStyle":true,"backmigrated":14};
calc.setState(graphState);
window.calc=calc;
};
document.head.appendChild(script);
function toggleFullscreen(){
const btn=document.getElementById('fsbtn');
const container=document.getElementById('calculator');
if(!document.fullscreenElement){
container.requestFullscreen?.();
btn.innerText="✕ 退出";
}else{
document.exitFullscreen?.();
btn.innerText="⛶ 全屏";
}
setTimeout(()=>window.calc?.resize(),120);
}
document.addEventListener('fullscreenchange',()=>{
const btn=document.getElementById('fsbtn');
if(document.fullscreenElement){
btn.innerText="✕ 退出";
}else{
btn.innerText="⛶ 全屏";
}
setTimeout(()=>window.calc?.resize(),120);
});
document.addEventListener('keydown',e=>{
if(e.key==='F11'){
e.preventDefault();
toggleFullscreen();
}
})
</script>

实例效果

存在问题

① 目前来看,还存在的唯一的问题,就是全屏功能切换时的短暂的黑屏现象,暂时搁置几天再看吧。

② 用另一个实例的操作中得到的经验,有时候用 JSON.stringify(Calc.getState()) 得到的代码,需要将其中的四个反斜线\\\\要替换为两个反斜线\\,代码才能成功执行,带有转义。

转化意义

有了 API 后,我们可以利用 AI 自动设计课件,不需要我们辛辛苦苦的自己一步一步亲自动手了,这一点我已经测试过了。比如告诉 AI ,“用 desmos 引入 api 的方法,制作一个椭圆围绕原点旋转45度的课件,要完整代码”。

posted @ 2026-11-07 19:54  静雅斋数学  阅读(16)  评论(0)    收藏  举报

休息时间到! ☕️

您已经专注工作了45分钟
建议起身活动或补充水分

—— 静雅斋
🟢 当前在线 0 | 🌍 国家/地区 0 | 👁 今日浏览 0 | 👥 访客 0 | 📌 访问 0 | 🚪 跳出率 0% | ⏱ 时长 0s | | 🌐 昨日浏览 0 | 👥 访客 0 | 📌 访问 0 | 🚪 跳出率 0% | ⏱ 时长 0s