canvas.uvue 28.7 KB
Newer Older
X
xty 已提交
1
<template>
2
  <view class="page" id="page-canvas">
3

4
    <canvas id="canvas" class="canvas-element"></canvas>
5
    <scroll-view class="scroll-view">
6 7 8 9 10 11 12 13 14 15 16
      <view>
        <text style="padding: 8px 10px;">获取 CanvasContext</text>
      </view>
      <view class="text-group">
        <text>获取 CanvasContext 结果:</text>
        <text id="testCanvasContext">{{testCanvasContext}}</text>
      </view>
      <view class="text-group">
        <text>测试 ToDataURL 结果:</text>
        <text id="testToDataURLResult">{{testToDataURLResult}}</text>
      </view>
17

18 19 20 21
      <view class="text-group">
        <text>测试 createImage 结果:</text>
        <text id="testCreateImage">{{testCreateImage}}</text>
      </view>
22

23 24 25 26 27
      <view class="text-group">
        <text>测试 createPath2D 结果:</text>
        <text id="testCreatePath2D">{{testCreatePath2D}}</text>
      </view>

28 29 30 31 32 33 34
      <!-- #ifdef WEB -->
      <button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button>
      <view>
        <text>testToBlobResult: {{testToBlobResult}}</text>
      </view>
      <!-- #endif -->
      <button class="canvas-drawing-button" id="toDataURL" @click="canvasToDataURL">canvasToDataURL</button>
35 36 37 38
      <view class="text-group" v-if="dataBase64.length>0">
        <text>canvasToDataURL:</text>
        <text>{{dataBase64.slice(0,22)}}...</text>
      </view>
39
      <button @click="onCreateImage">createImage</button>
40
      <button @click="onCreatePath2D">createPath2D</button>
41 42 43 44
      <view style="padding: 8px 10px;">CanvasContext API 演示</view>
      <view class="grid-view">
        <view class="grid-item" v-for="(name, index) in names" :key="index">
          <button class="canvas-drawing-button" @click="handleCanvasButton(name)">{{name}}</button>
45 46
        </view>
      </view>
47

48 49
    </scroll-view>
  </view>
50

51 52 53 54 55
</template>

<script>
  function hidpi(canvas : UniCanvasElement) {
    const context = canvas.getContext("2d")!;
56
    const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
57 58 59 60 61 62 63
    canvas.width = canvas.offsetWidth * dpr;
    canvas.height = canvas.offsetHeight * dpr;
    context.scale(dpr, dpr);
  }

  export default {
    data() {
64
      const API_PATH = ["arc", "arcTo", "bezierCurveTo", "quadraticCurve", "moveTo", "lineTo", "rect", "clip", "createPattern","getSetImageData"]
65 66
      const API_DRAW = ["stroke", "strokeRect", "strokeText", "fill", "fillRect", "fillText", "drawImage", "drawImageLocal", "clearRect"]
      const API_STATE = ["beginPath", "closePath", "reset", "transform", "rotate", "resetTransform", "save", "restore", "scale", "translate"]
67
      const API_PROPERTIES = ["lineCap", "lineJoin", "lineWidth", "miterLimit", "fillStyle", "strokeStyle", "globalAlpha", "font", "setLineDash", "createLinearGradient", "createRadialGradient", "textAlign"]
68

69
      return {
70
        title: 'Context2D',
71
        canvas: null as UniCanvasElement | null,
72 73
        canvasContext: null as CanvasContext | null,
        renderingContext: null as CanvasRenderingContext2D | null,
74
        canvasWidth: 0,
75
        canvasHeight: 0,
76
        dataBase64: '',
77
        // 仅测试
78
        testCanvasContext: false,
79
        testToBlobResult: false,
80
        testToDataURLResult: false,
81
        names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText", "path2D"].sort() as string[],
82 83
        // 仅测试
        testCreateCanvasContextAsyncSuccess: false,
84 85
        testCreateImage: false,
        testCreatePath2D: false
86 87 88
      }
    },
    onReady() {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
      // HBuilderX 4.25+
      // 异步调用方式, 跨平台写法
      uni.createCanvasContextAsync({
        id: 'canvas',
        component: this,
        success: (context : CanvasContext) => {
          this.canvasContext = context;
          this.renderingContext = context.getContext('2d')!;
          this.canvas = this.renderingContext!.canvas;

          hidpi(this.canvas!);
          this.canvasWidth = this.canvas!.width;
          this.canvasHeight = this.canvas!.height;

          // #ifdef WEB
          context.toBlob((blob : Blob) => {
            this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
          }, 'image/jpeg', 0.95)
          // #endif
          this.testToDataURLResult = context.toDataURL().startsWith('data:image/png;base64')
          this.testCanvasContext = true
        }
      })

      // 同步调用方式,仅支持 app/web
      // let canvas = uni.getElementById("canvas") as UniCanvasElement
      // this.renderingContext = canvas.getContext("2d")
      // hidpi(canvas);
      // this.canvas = canvas;
      // this.canvasWidth = canvas.width;
      // this.canvasHeight = canvas.height;
120 121
    },
    methods: {
122
      // #ifdef WEB
123
      canvasToBlob() {
124
        this.canvasContext!.toBlob((blob : Blob) => {
125 126 127
          this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
        }, 'image/jpeg', 0.95)
      },
128
      // #endif
129
      canvasToDataURL() {
130
        this.dataBase64 = this.canvasContext!.toDataURL()
131
      },
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      onCreateImage() {
        this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
        let image = this.canvasContext!.createImage();
        image.src = "../../static/logo.png"
        image.onload = () => {
          this.testCreateImage = true
          this.renderingContext?.drawImage(image, 0, 0, 100, 100);
        }
      },
      onCreatePath2D() {
        this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
        const context = this.renderingContext!
        let path2D = this.canvasContext!.createPath2D()
        this.testCreatePath2D = true
        const amplitude = 64;
        const wavelength = 64;
        for (let i = 0; i < 5; i++) {
          const x1 = 0 + (i * wavelength);
          const y1 = 128;
          const x2 = x1 + wavelength / 4;
          const y2 = y1 - amplitude;
          const x3 = x1 + 3 * wavelength / 4;
          const y3 = y1 + amplitude;
          const x4 = x1 + wavelength;
          const y4 = y1;
          context.moveTo(x1, y1);
          path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
        }
        context.stroke(path2D);
161 162
      },
      handleCanvasButton(name : string) {
163 164
        this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
        this.renderingContext!.save()
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        switch (name) {
          case "arc":
            this.arc();
            break;
          case "arcTo":
            this.arcTo();
            break;
          case "beginPath":
            this.beginPath();
            break;
          case "bezierCurveTo":
            this.bezierCurveTo();
            break;
          case "clearRect":
            this.clearRect();
            break;
          case "clip":
            this.clip();
            break;
          case "closePath":
            this.closePath();
            break;
187 188 189 190
          case "getSetImageData":
            this.getSetImageData();
            break;
          case "createPattern":
191 192
            this.pattern()
            break;
193
          case "createLinearGradient":
194 195
            this.createLinearGradient();
            break;
196
          case "createRadialGradient":
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
            this.createRadialGradient();
            break;
          case "fill":
            this.fill();
            break;
          case "fillRect":
            this.fillRect();
            break;
          case "fillText":
            this.fillText();
            break;
          case "lineTo":
            this.lineTo();
            break;
          case "moveTo":
            this.moveTo();
            break;
          case "quadraticCurve":
            this.quadraticCurveTo();
            break;
          case "rect":
            this.rect();
            break;
          case "reset":
            this.reset();
            break;
          case "resetTransform":
            this.resetTransform();
            break;
          case "restore":
            this.restore();
            break;
          case "rotate":
            this.rotate();
            break;
          case "save":
            this.save();
            break;
          case "scale":
            this.scale();
            break;
          case "stroke":
            this.stroke();
            break;
          case "strokeRect":
            this.strokeRect();
            break;
          case "strokeText":
            this.strokeText();
            break;
          case "transform":
            this.transform();
            break;
          case "translate":
            this.translate();
            break;
          case "drawImageLocal":
            this.drawImageLocal()
            break;
          case "drawImage":
            this.drawImage();
            break;
          case "measureText":
            this.measureText();
            break;
262
          case "fillStyle":
263 264
            this.setFillStyle();
            break;
265
          case "strokeStyle":
266 267
            this.setStrokeStyle();
            break;
268
          case "globalAlpha":
269 270
            this.setGlobalAlpha();
            break;
271
          case "font":
272 273
            this.setFontSize();
            break;
274
          case "lineCap":
275 276
            this.setLineCap();
            break;
277
          case "lineJoin":
278 279
            this.setLineJoin();
            break;
280
          case "setLineDash":
281 282
            this.lineDash();
            break;
283
          case "lineWidth":
284 285
            this.setLineWidth();
            break;
286
          case "miterLimit":
287 288 289
            this.setMiterLimit();
            break;
          case "textAlign":
290 291 292 293
            this.textAlign();
            break;
          case "path2D":
            this.path2DMethods();
H
hdx 已提交
294
            break;
295 296 297
          default:
            break;
        }
298 299 300 301 302 303 304 305
        this.renderingContext!.restore()
      },
      clearCanvasRect() {
        this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
      },
      clearCanvasRectAndRestore() {
        this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
        // this.renderingContext!.restore()
306
      },
307
      arc() {
308
        const context = this.renderingContext!
309 310 311 312 313 314 315 316 317 318 319 320

        context.beginPath()
        context.lineWidth = 2
        context.arc(75, 75, 50, 0, Math.PI * 2, true)
        context.moveTo(110, 75)
        context.arc(75, 75, 35, 0, Math.PI, false)
        context.moveTo(65, 65)
        context.arc(60, 65, 5, 0, Math.PI * 2, true)
        context.moveTo(95, 65)
        context.arc(90, 65, 5, 0, Math.PI * 2, true)
        context.stroke()
      },
321
      arcTo() {
322
        const context = this.renderingContext!
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

        context.beginPath()
        context.moveTo(150, 20)
        context.arcTo(150, 100, 50, 20, 30)
        context.stroke()

        context.fillStyle = "blue"
        // base point
        context.fillRect(150, 20, 10, 10)

        context.fillStyle = "red"
        // control point one
        context.fillRect(150, 100, 10, 10)
        // control point two
        context.fillRect(50, 20, 10, 10)
        //
        context.setLineDash([5, 5])
        context.moveTo(150, 20)
        context.lineTo(150, 100)
        context.lineTo(50, 20)
        context.stroke()
        context.beginPath()
        context.arc(120, 38, 30, 0, 2 * Math.PI, true)
        context.stroke()
      },
      beginPath() {
349
        const context = this.renderingContext!
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

        // First path
        context.beginPath()
        context.strokeStyle = "blue"
        context.moveTo(20, 20)
        context.lineTo(200, 20)
        context.stroke()

        // Second path
        context.beginPath()
        context.strokeStyle = "green"
        context.moveTo(20, 20)
        context.lineTo(120, 120)
        context.stroke()
      },
      textAlign() {
366
        const context = this.renderingContext!
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

        context.beginPath()
        context.moveTo(150, 0)
        context.lineTo(150, 150)
        context.stroke()

        context.font = "30px serif"

        context.textAlign = "left"
        context.fillText("left-aligned", 150, 40)

        context.textAlign = "center"
        context.fillText("center-aligned", 150, 85)

        context.textAlign = "right"
        context.fillText("right-aligned", 150, 130)
      },
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
      getSetImageData(){
        const context = this.renderingContext!
        // Create path
        context.moveTo(0, 70);
        context.lineTo(80, 0);
        context.lineTo(210, 110);
        context.lineTo(30, 110);
        context.lineTo(160, 0);
        context.lineTo(240, 70);
        context.closePath();

        // Fill path
        context.fillStyle = "green";
        context.fill();

        let imageData = context.getImageData(0, 0, context.canvas.width/2, context.canvas.height/2)
        context.putImageData(imageData, context.canvas.width/2, context.canvas.height/2)
      },
402
      bezierCurveTo() {
403
        const context = this.renderingContext!
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

        context.beginPath()
        context.moveTo(50, 20)
        context.bezierCurveTo(230, 30, 150, 60, 50, 100)
        context.stroke()

        context.fillStyle = "blue"
        // start point
        context.fillRect(50, 20, 10, 10)
        // end point
        context.fillRect(50, 100, 10, 10)

        context.fillStyle = "red"
        // control point one
        context.fillRect(230, 30, 10, 10)
        // control point two
        context.fillRect(150, 70, 10, 10)
      },
      clearRect() {
423
        const context = this.renderingContext!
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

        // 绘制黄色背景
        context.beginPath()
        context.fillStyle = "#ff6"
        context.fillRect(0, 0, 300, 150)

        // 绘制蓝色三角形
        context.beginPath()
        context.fillStyle = "blue"
        context.moveTo(20, 20)
        context.lineTo(180, 20)
        context.lineTo(130, 130)
        context.closePath()
        context.fill()

        // 清除一部分画布
        context.clearRect(10, 10, 120, 100)
      },
      clip() {
443
        const context = this.renderingContext!
444 445 446 447 448 449 450 451 452 453 454 455 456

        // Create circular clipping region
        context.beginPath();
        context.arc(100, 75, 50, 0, Math.PI * 2, true)
        context.clip()

        // Draw stuff that gets clipped
        context.fillStyle = "blue"
        context.fillRect(0, 0, 300, 150)
        context.fillStyle = "orange"
        context.fillRect(0, 0, 100, 100)
      },
      closePath() {
457
        const context = this.renderingContext!
458 459 460 461 462 463 464 465 466 467

        context.beginPath()
        context.lineWidth = 10
        context.moveTo(20, 20)
        context.lineTo(20, 100)
        context.lineTo(70, 100)
        context.closePath()
        context.stroke()
      },
      pattern() {
468
        const context = this.renderingContext!
469 470 471 472 473 474 475 476

        const image = new Image(100, 100)
        image.src = '../../../static/api.png';
        image.onload = () => {
          context.save()
          this.clearCanvasRect()
          const pattern = context.createPattern(image, "repeat")
          context.fillStyle = pattern
477
          context.fillRect(0, 0, 400, 400)
478 479 480 481
          context.restore()
        };
      },
      createLinearGradient() {
482
        const context = this.renderingContext!
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

        // Create a linear gradient
        // The start gradient point is at x=20, y=0
        // The end gradient point is at x=220, y=0
        const gradient = context.createLinearGradient(20, 0, 220, 0)

        // Add three color stops
        gradient.addColorStop(0, "green")
        gradient.addColorStop(0.5, "cyan")
        gradient.addColorStop(1, "green")

        // Set the fill style and draw a rectangle
        context.fillStyle = gradient
        context.fillRect(20, 20, 200, 100)
      },
      createRadialGradient() {
499
        const context = this.renderingContext!
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

        // Create a radial gradient
        // The inner circle is at x=110, y=90, with radius=30
        // The outer circle is at x=100, y=100, with radius=70
        const gradient = context.createRadialGradient(110, 90, 30, 100, 100, 70)

        // Add three color stops
        gradient.addColorStop(0, "pink")
        gradient.addColorStop(0.9, "white")
        gradient.addColorStop(1, "green")

        // Set the fill style and draw a rectangle
        context.fillStyle = gradient
        context.fillRect(20, 20, 160, 160)
      },
      fill() {
516
        const context = this.renderingContext!
517 518 519 520 521 522 523

        context.beginPath()
        context.rect(20, 20, 150, 100)
        context.strokeStyle = '#00ff00'
        context.fill()
      },
      fillRect() {
524
        const context = this.renderingContext!
525 526 527 528 529

        context.fillStyle = "green"
        context.fillRect(20, 10, 150, 100)
      },
      fillText() {
530
        const context = this.renderingContext!
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

        context.strokeStyle = '#ff0000'

        context.beginPath()
        context.moveTo(0, 10)
        context.lineTo(300, 10)
        context.stroke()
        // context.save()
        // context.scale(1.5, 1.5)
        // context.translate(20, 20)
        // context.setFontSize(10)
        context.fillText('Hello World', 0, 30, 300)
        // context.setFontSize(20)
        context.fillText('Hello World', 100, 30, 300)

        // context.restore()

        context.beginPath()
        context.moveTo(0, 30)
        context.lineTo(300, 30)
        context.stroke()
      },
      moveTo() {
554
        const context = this.renderingContext!
555 556 557 558 559 560 561

        context.beginPath()
        context.moveTo(0, 0)
        context.lineTo(300, 150)
        context.stroke()
      },
      lineTo() {
562
        const context = this.renderingContext!
563 564 565 566 567 568 569 570

        context.beginPath()
        context.moveTo(20, 20)
        context.lineTo(20, 100)
        context.lineTo(70, 100)
        context.stroke()
      },
      stroke() {
571
        const context = this.renderingContext!
572 573 574 575 576 577 578 579 580

        context.beginPath()
        context.moveTo(20, 20)
        context.lineTo(20, 100)
        context.lineTo(70, 100)
        context.strokeStyle = '#00ff00'
        context.stroke()
      },
      strokeRect() {
581
        const context = this.renderingContext!
582 583 584 585 586

        context.strokeStyle = "green"
        context.strokeRect(20, 10, 160, 100)
      },
      strokeText() {
587
        const context = this.renderingContext!
588 589 590 591 592 593 594 595 596

        context.font = "10px serif"
        context.strokeText("Hello world", 50, 90)

        context.font = "30px serif"
        context.strokeStyle = "blue"
        context.strokeText("Hello world", 50, 100)
      },
      rotate() {
597
        const context = this.renderingContext!
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

        // Point of transform origin
        context.arc(0, 0, 5, 0, 2 * Math.PI, true)
        context.fillStyle = "blue"
        context.fill()

        // Non-rotated rectangle
        context.fillStyle = "gray"
        context.fillRect(100, 0, 80, 20)
        // Rotated rectangle
        context.rotate((45 * Math.PI) / 180)
        context.fillStyle = "red"
        context.fillRect(100, 0, 80, 20)

        // Reset transformation matrix to the identity matrix
        context.setTransform(1, 0, 0, 1, 0, 0)
      },
      scale() {
616
        const context = this.renderingContext!
617 618 619 620 621 622 623 624 625 626 627 628 629 630

        // Scaled rectangle
        context.scale(9, 3)
        context.fillStyle = "red"
        context.fillRect(10, 10, 8, 20)

        // Reset current transformation matrix to the identity matrix
        context.setTransform(1, 0, 0, 1, 0, 0)

        // Non-scaled rectangle
        context.fillStyle = "gray"
        context.fillRect(10, 10, 8, 20)
      },
      reset() {
631
        const context = this.renderingContext!
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

        // Set line width
        context.lineWidth = 10
        context.strokeStyle = '#00ff00'
        // Stroke rect outline
        context.strokeRect(50, 50, 150, 100)

        // Create filled text
        context.font = "50px serif";
        context.fillText("Rect!", 70, 110)

        context.lineWidth = 5

        // Stroke out circle
        context.beginPath();
        context.arc(300, 100, 50, 0, 2 * Math.PI)
        context.stroke();

        // Create filled text
        context.font = "25px sans-serif"
        context.fillText("Circle!", 265, 100)
        // context.reset()

        hidpi(uni.getElementById("canvas") as UniCanvasElement)
      },
      translate() {
658
        const context = this.renderingContext!
659 660 661 662 663 664 665 666 667 668 669 670 671 672

        // Moved square
        context.translate(110, 30)
        context.fillStyle = "red"
        context.fillRect(0, 0, 80, 80)

        // Reset current transformation matrix to the identity matrix
        context.setTransform(1, 0, 0, 1, 0, 0)

        // Unmoved square
        context.fillStyle = "gray"
        context.fillRect(0, 0, 80, 80)
      },
      save() {
673 674
        const context = this.renderingContext!

675 676 677 678 679 680 681 682 683 684 685 686 687
        context.beginPath()
        context.strokeStyle = '#00ff00'
        context.scale(2, 2)
        context.strokeStyle = '#ff0000'
        context.rect(0, 0, 100, 100)
        context.stroke()
        context.restore()

        context.save()
        context.rect(0, 0, 50, 50)
        context.stroke()
      },
      restore() {
688 689
        const context = this.renderingContext!;

690 691 692 693 694 695 696 697 698 699
        [3, 2, 1].forEach((item) => {
          context.save()
          context.beginPath()
          context.scale(item, item)
          context.rect(10, 10, 100, 100)
          context.stroke()
          context.restore()
        })
      },
      drawImageLocal() {
700
        const context = this.renderingContext!
701 702 703 704 705 706
        const image = new Image(100, 100)
        image.src = '../../../static/uni.png'
        image.onload = () => {
          context.drawImage(image, 0, 0, 100, 100)
        }
      },
707
      drawImage() {
708
        const context = this.renderingContext!
709 710 711 712 713 714 715
        const image = new Image(100, 100);
        image.src = 'https://web-ext-storage.dcloud.net.cn/uni-app-x/hello-uniappx-qrcode.png'
        image.onload = () => {
          context.drawImage(image, 0, 0, 100, 100)
        }
      },
      rect() {
716
        const context = this.renderingContext!
717 718 719 720 721 722

        context.beginPath()
        context.rect(20, 20, 150, 100)
        context.stroke()
      },
      quadraticCurveTo() {
723
        const context = this.renderingContext!
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

        // Quadratic Bézier curve
        context.beginPath()
        context.moveTo(50, 20)
        context.quadraticCurveTo(230, 30, 50, 100)
        context.stroke()

        // Start and end points
        context.fillStyle = "blue"
        context.beginPath()
        context.arc(50, 20, 5, 0, 2 * Math.PI, true) // Start point
        context.arc(50, 100, 5, 0, 2 * Math.PI, true) // End point
        context.fill();

        // Control point
        context.fillStyle = "red"
        context.beginPath()
        context.arc(230, 30, 5, 0, 2 * Math.PI, true)
        context.fill()
      },
      resetTransform() {
745
        const context = this.renderingContext!
746 747 748 749 750 751 752 753 754 755 756

        // Draw a rotated rectangle
        context.rotate((45 * Math.PI) / 180)
        context.fillRect(60, 0, 100, 30)

        // Reset transformation matrix to the identity matrix
        context.resetTransform()
        context.fillStyle = "red"
        context.fillRect(60, 0, 100, 30)
      },
      transform() {
757
        const context = this.renderingContext!
758 759 760 761 762

        context.transform(1, 0.2, 0.8, 1, 0, 0)
        context.fillRect(0, 0, 100, 100)
      },
      setFillStyle() {
763
        const context = this.renderingContext!;
764 765 766 767 768 769 770 771
        ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
          context.fillStyle = item
          context.beginPath()
          context.rect(0 + 75 * index, 0, 50, 50)
          context.fill()
        })
      },
      setStrokeStyle() {
772
        const context = this.renderingContext!;
773 774 775 776 777 778 779 780 781

        ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
          context.strokeStyle = item
          context.beginPath()
          context.rect(0 + 75 * index, 0, 50, 50)
          context.stroke()
        })
      },
      setGlobalAlpha() {
782
        const context = this.renderingContext!
783 784 785 786 787 788 789 790 791 792 793

        context.fillStyle = '#000000';
        [1, 0.5, 0.1].forEach((item : number, index : number) => {
          context.globalAlpha = item
          context.beginPath()
          context.rect(0 + 75 * index, 0, 50, 50)
          context.fill()
        })
        context.globalAlpha = 1
      },
      setFontSize() {
794
        const context = this.renderingContext!;
795 796 797 798 799 800 801

        [10, 20, 30, 40].forEach((item : number, index : number) => {
          context.font = item + 'px serif'
          context.fillText('Hello, world', 20, 20 + 40 * index)
        })
      },
      setLineCap() {
802
        const context = this.renderingContext!
803 804 805 806 807 808 809 810 811 812 813

        context.lineWidth = 10;
        ['butt', 'round', 'square'].forEach((item : string, index : number) => {
          context.beginPath()
          context.lineCap = item
          context.moveTo(20, 20 + 20 * index)
          context.lineTo(100, 20 + 20 * index)
          context.stroke()
        })
      },
      setLineJoin() {
814
        const context = this.renderingContext!
815 816 817 818 819 820 821 822 823 824 825 826

        context.lineWidth = 10;
        ['bevel', 'round', 'miter'].forEach((item : string, index : number) => {
          context.beginPath()
          context.lineJoin = item
          context.moveTo(20 + 80 * index, 20)
          context.lineTo(100 + 80 * index, 50)
          context.lineTo(20 + 80 * index, 100)
          context.stroke()
        })
      },
      setLineWidth() {
827
        const context = this.renderingContext!;
828 829 830 831 832 833 834 835 836 837

        [2, 4, 6, 8, 10].forEach((item : number, index : number) => {
          context.beginPath()
          context.lineWidth = item
          context.moveTo(20, 20 + 20 * index)
          context.lineTo(100, 20 + 20 * index)
          context.stroke()
        })
      },
      lineDash() {
838
        const context = this.renderingContext!
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856

        context.setLineDash([4, 16])

        // Dashed line with no offset
        context.beginPath()
        context.moveTo(0, 50)
        context.lineTo(300, 50)
        context.stroke()

        // Dashed line with offset of 4
        context.beginPath()
        context.strokeStyle = "red"
        context.lineDashOffset = 4
        context.moveTo(0, 100)
        context.lineTo(300, 100)
        context.stroke()
      },
      setMiterLimit() {
857
        const context = this.renderingContext!
858 859 860 861 862 863 864 865 866 867 868 869

        context.lineWidth = 4;
        [2, 4, 6, 8, 10].forEach((item : number, index : number) => {
          context.beginPath()
          context.miterLimit = item
          context.moveTo(20 + 80 * index, 20)
          context.lineTo(100 + 80 * index, 50)
          context.lineTo(20 + 80 * index, 100)
          context.stroke()
        })
      },
      measureText() {
870
        const context = this.renderingContext!
871

872 873 874
        const text = "uni-app x,是下一代 uni-app,是一个跨平台应用开发引擎"

        context.font = "20px 宋体"
875

876 877 878 879 880
        context.fillStyle = "red"
        context.fillText(text, 0, 60)
        const textMetrics = context.measureText(text)
        context.strokeText(text, 40, 100)
        context.fillText("measure text width:" + textMetrics.width, 40, 80)
881
      },
H
hdx 已提交
882
      path2DMethods() {
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
        const context = this.renderingContext!

        context.beginPath()
        const path2D = new Path2D();
        const amplitude = 64;
        const wavelength = 64;
        for (let i = 0; i < 5; i++) {
          const x1 = 0 + (i * wavelength);
          const y1 = 128;
          const x2 = x1 + wavelength / 4;
          const y2 = y1 - amplitude;
          const x3 = x1 + 3 * wavelength / 4;
          const y3 = y1 + amplitude;
          const x4 = x1 + wavelength;
          const y4 = y1;
          context.moveTo(x1, y1);
          path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
        }
        context.stroke(path2D);

        const path2DRect = new Path2D();
        path2DRect.rect(10, 10, 100, 20);
        context.fill(path2DRect);
906 907 908
      }
    }
  }
X
xty 已提交
909 910 911
</script>

<style>
912 913 914 915 916
  .page {
    flex: 1;
    height: 100%;
    overflow: hidden;
  }
X
xty 已提交
917

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
  .scroll-view {
    flex: 1;
  }

  .canvas-element {
    width: 100%;
    height: 250px;
    background-color: #ffffff;
  }

  .grid-view {
    padding: 10px;
    flex-direction: row;
    flex-wrap: wrap;
  }

  .btn-to-image {
    margin: 10px;
  }
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

  .grid-item {
    width: 50%;
    padding: 5px;
  }

  .btn-to-image {
    margin: 10px;
  }

  .text-group {
    display: flex;
    flex-flow: row nowrap;
    justify-content: space-between;
    align-items: center;
    padding: 8px 10px;
  }
X
xty 已提交
954
</style>