2017-06-20 49 views
1

我很新的編程,我一直在遵循本教程在Ionic中製作一個簡單的繪圖應用程序。基於取向的離子調整畫布大小

我按照教程,並能夠使其工作。但是,當我在瀏覽器上使用離子服務器和響應式設備模式對其進行測試時,畫布在更改方向後開始繪製後不會進行調整。我開始在縱向視圖中繪圖,然後當我翻轉方向或橫向並再次開始繪製時,我可以清楚地看到畫布的切割點。

我附上了截圖和下面的代碼。

帆布draw.html

<ion-toolbar id="top-toolbar"> 
    <ion-buttons left> 
    <button *ngFor="let colour of availableColours" icon-only ion-button (click)="changeColour(colour)"> 
     <ion-icon [style.color]="colour" name="brush"></ion-icon> 
    </button> 
    </ion-buttons> 
    <ion-buttons right> 
    <button style="border: 1px solid #cecece;" ion-button icon-only style.color="#fff" (click)="changeColour('#fff')"> 
     <ion-icon style="color: #fff;" name="square"></ion-icon> 
    </button> 
    </ion-buttons> 
</ion-toolbar> 

<canvas #myCanvas (touchstart)="handleStart($event)" (touchmove)="handleMove($event)" (touched)="handleEnd($event)"></canvas> 

<ion-toolbar id="bottom-toolbar"> 
    <ion-buttons left> 
    <button color="dark" ion-button icon-only (click)="clearCanvas()"><ion-icon name="trash"></ion-icon></button> 
    </ion-buttons> 
    <ion-buttons right> 
    <button color="dark" ion-button icon-only (click)="changeSize(5)"><ion-icon style="font-size: 0.25em;" name="radio-button-on"></ion-icon></button> 
    <button color="dark" ion-button icon-only (click)="changeSize(10)"><ion-icon style="font-size: 0.5em;" name="radio-button-on"></ion-icon></button> 
    <button color="dark" ion-button icon-only (click)="changeSize(20)"><ion-icon style="font-size: 1em;" name="radio-button-on"></ion-icon></button> 
    <button color="dark" ion-button icon-only (click)="changeSize(50)"><ion-icon style="font-size: 2em;" name="radio-button-on"></ion-icon></button> 
    <button color="dark" ion-button icon-only (click)="changeSize(200)"><ion-icon style="font-size: 3em;" name="radio-button-on"></ion-icon></button> 
    </ion-buttons> 
</ion-toolbar> 

帆布draw.module.ts

import { NgModule } from '@angular/core'; 
import { IonicPageModule } from 'ionic-angular'; 
import { CanvasDraw } from './canvas-draw'; 

@NgModule({ 
    declarations: [ 
    CanvasDraw, 
    ], 
    imports: [ 
    IonicPageModule.forChild(CanvasDraw), 
    ], 
    exports: [ 
    CanvasDraw 
    ] 
}) 
export class CanvasDrawComponentModule {} 

帆布draw.scss

canvas-draw { 
    height: 100%; 
    width: 100%; 
    display: block; 
    #top-toolbar{ 
     position: absolute; 
     top: 0; 
    } 
    #bottom-toolbar{ 
     position: absolute; 
     bottom: 0; 
    } 
} 

帆布draw.ts

import { Component,ViewChild,Renderer } from '@angular/core'; 
import { Platform } from 'ionic-angular'; 

@Component({ 
    selector: 'canvas-draw', 
    templateUrl: 'canvas-draw.html' 
}) 
export class CanvasDraw { 
    @ViewChild('myCanvas') canvas: any; 

    canvasElement: any; 
    lastX: number; 
    lastY: number; 

    currentColour: string = '#1abc9c'; 
    availableColours: any; 

    brushSize: number = 10; 

    constructor(public platform: Platform, public renderer: Renderer) { 
     console.log('Hello CanvasDraw Component'); 

     this.availableColours = [ 
      '#1abc9c', 
      '#3498db', 
      '#9b59b6', 
      '#e67e22', 
      '#e74c3c' 
     ]; 

    } 

    ngAfterViewInit(){ 

     this.canvasElement = this.canvas.nativeElement; 

     this.renderer.setElementAttribute(this.canvasElement, 'width', this.platform.width() + ''); 
     this.renderer.setElementAttribute(this.canvasElement, 'height', this.platform.height() + ''); 

    } 

    changeColour(colour){ 
     this.currentColour = colour; 
    } 

    changeSize(size){ 
     this.brushSize = size; 
    } 

    handleStart(ev){ 

     this.lastX = ev.touches[0].pageX; 
     this.lastY = ev.touches[0].pageY; 
    } 

    handleMove(ev){ 

     let ctx = this.canvasElement.getContext('2d'); 
     let currentX = ev.touches[0].pageX; 
     let currentY = ev.touches[0].pageY; 

     ctx.beginPath(); 
     ctx.lineJoin = "round"; 
     ctx.moveTo(this.lastX, this.lastY); 
     ctx.lineTo(currentX, currentY); 
     ctx.closePath(); 
     ctx.strokeStyle = this.currentColour; 
     ctx.lineWidth = this.brushSize; 
     ctx.stroke();  

     this.lastX = currentX; 
     this.lastY = currentY; 

    } 

    handleEnd(ev){ 

     console.log(ev); 
    } 

    clearCanvas(){ 
     let ctx = this.canvasElement.getContext('2d'); 
     ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height); 
    } 

} 

截圖

首先我畫在縱向視圖。

portrait view

然後翻轉方向和我繼續繪製,但你能看到畫布被切斷。

canvas is cut off

+0

這是我正在使用的教程的鏈接:https://www.joshmorony.com/creating-a-drawing-application-in-ionic/ –

回答

0

所以我下面的代碼添加到ngAfterViewInit,那就是能夠調整畫布大小,當方向沒有任何改變截斷,但圖紙被擦除,所以我還是想,如果找出來有一種保持繪圖的方法。

window.addEventListener("resize", (e) => { 
    this.canvasElement.width = window.innerWidth; 
    this.canvasElement.height = window.innerHeight;