|
参考canvas教程里的代码:
- <template>
- <div class="doc-page">
- <div class="content">
- <canvas class="new_canvas" id="newCanvas"></canvas>
- </div>
- </div>
- </template>
-
- <style>
- .content {
- flex-direction: column;
- align-items: center;
- border-width: 10px;
- border-color: #0000FF;
- width: 100%;
- }
- .new_canvas {
- border-width: 5px;
- border-color: #2F4F4F;
- height: 380px;
- width: 380px;
- }
- </style>
-
- <script>
- export default {
- private:{
- },
- onInit(){
- },
- onShow(){
- this.drawCanvas();
- },
- drawCanvas(){
- const canvas = this.$element('newCanvas'); //获取 canvas 组件
- const ctx = canvas.getContext('2d'); //获取 canvas 绘图上下文
-
- console.log(canvas);
- console.log(canvas.style);
- console.log(JSON.stringify(canvas.attr));
- console.log(canvas.height);
-
- var r = 20;
- var h = 380;
- var p = Math.PI;
-
- ctx.beginPath();
- ctx.moveTo(r * 2, r);
- ctx.arc(r * 2, r * 2, r, -p / 2, -p, true);
- ctx.lineTo(r, h - r * 2);
- ctx.arc(r * 2, h - r * 2, r, p, p / 2, true);
- ctx.lineTo(h - r * 2, h - r);
- ctx.arc(h - r * 2, h - r * 2, r, p / 2, 0, true);
- ctx.lineTo(h - r, r * 2);
- ctx.arc(h - r * 2, r * 2, r, 0, -p / 2, true);
- ctx.closePath();
- ctx.stroke();
-
- var s = 60;
-
- ctx.beginPath();
- ctx.moveTo(h / 2 + s, h / 2);
- ctx.arc(h / 2, h / 2, s, 0, -p / 2 * 3, true);
- ctx.arc(h / 2, h / 2 + s + s / 2, s / 2, -p / 2, p / 2, false);
- ctx.arc(h / 2, h / 2, s * 2, -p / 2 * 3, 0, false);
- ctx.arc(h / 2 + s + s / 2, h / 2, s / 2, 0, p, false);
- ctx.moveTo(h / 2 + s * 2, h / 2 + s + s / 2);
- ctx.arc(h / 2 + s + s / 2, h / 2 + s + s / 2, s / 2, 0, p * 2, false);
- ctx.moveTo(h / 2 + s / 4 * 3, h / 2 + s / 2);
- ctx.arc(h / 2 + s / 2, h / 2 + s / 2, s / 4, 0, p * 2, false);
- ctx.fill();
- }
- }
- </script>
复制代码 同一个页面进去再出来,来回几次画出来的东西就不一样了,见图:
|
-
-
|