gpt4 book ai didi

angular - 根据提供的json动态生成多个 Canvas 区域

转载 作者:太空狗 更新时间:2023-10-29 17:54:33 24 4
gpt4 key购买 nike

我正在使用 angular2 和 html5 Canvas 。基于提供的 json,我想创建多个带有 Canvas 区域的 div。

这就是如何用 Canvas 区域生成我的 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;
}

下面是我的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,两个 Canvas 都应该有一些形状,但在我的例子中,第二个 Canvas 是空的

最佳答案

尝试使用@ViewChildren 而不是@ViewChild 并使用

遍历每个 Canvas
let canvas = this.myCanvas.toArray()[i].nativeElement;

那么谁将有权访问动态创建的每个 Canvas 区域。

关于angular - 根据提供的json动态生成多个 Canvas 区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com