@makemogic/runtime
v0.9.4
Published
Mogic Runtime Libra
Downloads
22
Readme
@makemogic/runtime
用于构建 Mogic 平台交互式应用的运行时库。
使用
import { mogicRuntime } from '@makemogic/runtime'Tweaks (调整参数)
创建可在父应用中实时调整的可配置参数。
import remixConfig from './remix.json'
const tweaks = mogicRuntime.tweaks(remixConfig)
// 在 React 组件中使用
function App() {
const bgColor = tweaks.bg.useState()
const speed = tweaks.speed.useState()
return <div style={{ backgroundColor: bgColor }}>Speed: {speed}</div>
}
// 编程式更新
mogicRuntime.updateTweaks({ bg: '#00FF00', speed: 5 })支持类型: text, slider, toggle, color, select, theme, image/video/audio
注意: 每个应用仅调用一次 tweaks()。
文本生成
const response = await mogicRuntime.generateText({
prompt: 'Write a short story'
})
console.log(response.text)选项: prompt (必填)
图片生成
const response = await mogicRuntime.generateImage({
prompt: 'A beautiful sunset',
aspectRatio: '16:9',
referenceImages: ['https://example.com/image1.jpg']
})
const img = document.createElement('img')
img.src = response.img?.url ?? ''
document.body.appendChild(img)选项: prompt (必填), aspectRatio, referenceImages
文件上传
const file = fileInput.files[0]
const response = await mogicRuntime.uploadFile(file)
console.log(response.url)触觉反馈
mogicRuntime.performHaptic('medium')震动强度: 'light', 'medium', 'heavy', 'rigid', 'soft'
运动与摇动检测
const unsubscribe = mogicRuntime.addMotionListener((motion) => {
console.log(motion)
})
const removeShake = mogicRuntime.addShakeListener(() => {
console.log('Shaken!')
})
// 清理监听
unsubscribe()
removeShake()销毁实例
mogicRuntime.destroy()