<script type="text/template" id="template">
<view id="container">
<text id="testText" class="redText" value="hello canvas"></text>
</view>
</script>
<script id="style">
window.styleValue = {
container: {
width: 800,
height: 400,
backgroundColor: '#f3f3f3',
justifyContent: 'center',
alignItems: 'center',
},
testText: {
color: '#ffffff',
width: '100%',
height: '100%',
lineHeight: 400,
fontSize: 80,
textAlign: 'center',
},
// 文字的最终颜色为#ff0000
redText: {
color: '#ff0000',
}
}
</script>
<canvas id="canvas">
xxxxxxxxxx
import Layout from "https://cdn.skypack.dev/minigame-canvas-engine";
const HelloPlugin = {
install(Layout) {
Layout.sayHello = () => {
console.log('Hello Layout Plugin');
}
},
uninstall(Layout) {
delete Layout.sayHello;
},
name: 'Hello'
}
Layout.use(HelloPlugin);
Layout.sayHello();
Layout.unUse(HelloPlugin);
try {
Layout.sayHello();
} catch(e) {
console.log(e.message); // "Layout.sayHello is not a function"
}
/**
* 只需要在Web端调试场景下引用
* 引入minigame-canvas-engine-devtools之后,layoutDevtools会自动挂载到windows
*/
import LayoutDevTools from "https://cdn.skypack.dev/minigame-canvas-engine-devtools";
window.layoutDevtools.init(Layout);
const template = document.getElementById('template').innerHTML;
const style = eval(document.getElementById("style").innerHTML);
let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');
Layout.init(template, style);
const container = Layout.getElementsById('container')[0];
// 设置canvas的尺寸和样式的container比例一致
canvas.style.width = container.style.width / 2 + 'px';
canvas.style.height = container.style.height / 2 + 'px';
canvas.width = container.style.width;
canvas.height = container.style.height;
Layout.updateViewPort(canvas.getBoundingClientRect());
Layout.layout(context);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.