angular2 및 html5 캔버스를 사용하고 있습니다. 제공된 JSON을 기반으로 내부에 캔버스 영역이있는 여러 div를 만들고 싶습니다.json을 기반으로 여러 캔버스 영역을 동적으로 생성합니다.
이 내 JSON
masterPages = [{
areas: [
{
type: "FlowArea",
width: "183.0",
x: "12.0",
y: "40.0",
id: "FAR.1",
height: "237.0"
},
{
type: "StaticArea",
width: "210.0",
x: "0.0",
y: "7.0",
id: "SAR.2",
height: "25.0"
},
{
type: "StaticArea",
width: "210.0",
x: "0.0",
y: "282.0",
id: "SAR.1",
height: "15.0"
},
{
type: "StaticArea",
width: "2.0",
x: "6.0",
y: "26.0",
id: "SAR.3",
height: "256.0"
}
]
},
{
areas: [
{
type: "FlowArea",
width: "183.0",
x: "12.0",
y: "13.25",
id: "FAR.1",
height: "265.0"
},
{
type: "StaticArea",
width: "210.0",
x: "0.0",
y: "282.0",
id: "SAR.1",
height: "15.0"
}
]
}
];
는 JSON 모두 캔버스에 따라되는 캔버스 영역 내 div의 (HTML 코드)
<div class="masterpage" *ngFor="let x of masterPages" draggable [dragScope]="'masterpage'" [dragData]="x">
<canvas #myCanvas width="105" height="149" style="background:lightgray;"></canvas>
</div>
.TS 코드 아래
ngAfterViewInit() {
let canvas = this.myCanvas.nativeElement;
this.context = canvas.getContext("2d");
this.tick(this.masterPages);
}
tick(masterPages) {
var ctx = this.context;
ctx.clearRect(0, 0, 150, 150);
for (let j = 0; j < this.masterPages[this.count].areas.length; j++) {
ctx.fillStyle = this.masterPages[this.count].areas[j].type;
ctx.fillRect(masterPages[this.count].areas[j].x/2, masterPages[this.count].areas[j].y/2, masterPages[this.count].areas[j].width/2, masterPages[this.count].areas[j].height/2);
}
this.count = this.count + 1;
}
을 생성하는 방법이다 몇 가지 모양이 있어야하지만 제 경우에는 두 번째 캔버스가 비어 있습니다.
감사합니다. 나를 위해 일했습니다. – dsnr