Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="canvas"></canvas>

<script type="text/template" id="template">
  <view class="container" id="main">
     <scrollview class="richcontainer" scrollY="true" >
       <richtext id="rich"></richtext>
    </scrollview>
  </view>
</script>

<script id="style">
  window.styleValue = {
    container: {
      width: 600,
      height: 600,
      padding: 50,
      borderRadius: 12,
      backgroundColor: '#f3f3f3',
    },
    
    richcontainer: {
      width: 500,
      height: 500,
    },

    rich: {
      width: 500,
      height: 500,
      fontSize: 30,
      lineHeight: 36,
    }
  }
</script>
              
            
!

CSS

              
                
              
            
!

JS

              
                import * as dot from "https://cdn.skypack.dev/dot@1.1.3";
import Layout from "https://cdn.skypack.dev/minigame-canvas-engine";
import RichText from "https://cdn.skypack.dev/minigame-canvas-engine-richtext";

Layout.use(RichText);

/**
 * 只需要在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 tempFn = dot.template(template);
let resultText = tempFn({});

function init() {
    let canvas = document.getElementById("canvas");
    let context = canvas.getContext("2d");

    // 每次初始化之前先执行清理逻辑保证内存不会一直增长
    Layout.clear();
    // 初始化引擎
    Layout.init(resultText, style);

  // 设置canvas的尺寸和样式的container比例一致
  canvas.style.width = 1410 / 2 + 'px';
  canvas.style.height = 960 / 2 + 'px';
  canvas.width = 1410;
  canvas.height = 960;

  Layout.updateViewPort(canvas.getBoundingClientRect());

  Layout.layout(context);

  const rich = Layout.getElementsById('rich')[0];
  
  rich.text = `
  <p>这是一段富文本测试</p>
  <br>
  <strong>这是一段加粗的标题</strong>
  <p>这里展示了<strong>嵌套</strong>的标签</p>
  <br>
  <p>前面是一个换行</p>
  <p>文字可以自定义<span style="color: red">颜色</span>
  <br>
  <p style="font-weight: 300">样式可以<span style="color: blue">继承</span>,也可以<span style="font-weight: bold">自定义</span>,这段很长的文字会自动换行,富文本组件都会自动处理好</p>
  <p style="text-align: center">这是一段居中的文本,居中的文本暂不支持内嵌标签</p>
  `;
}

init();

              
            
!
999px

Console