简体   繁体   English

金字塔中的Fabric.js图像

[英]Fabric.js Images in a pyramid

I have some very wet code with coordinates for to make a pyramid with a wide base, out of images in frabric.js 我有一些非常湿的代码,其坐标用于根据frabric.js中的图像制作具有宽底的金字塔

I have tried all day to succeed in writing a loop to dry up the code. 我整天都在尝试成功编写循环以使代码干燥。 I'm somewhat of a beginner, so I cannot get it into my head how to do this. 我有点初学者,所以我不知道该怎么做。

Please can someone help. 请有人帮忙。 I've looked around and found how to do something similar with circles in fabric.js, but I can't seem to translate it into images. 我环顾四周, 发现如何对 fabric.js中的圆圈执行类似的操作,但是我似乎无法将其转换为图像。

The loop also needs to increment a row number, so fabric knows when to add the next row. 循环还需要增加行号,因此结构知道何时添加下一行。

This is the terrible code I have. 这是我的糟糕代码。

var canvasWidth = 1000;
var imageWidth = 20;
var row1 = 1;
var a = canvasWidth/2-(imageWidth)*1/2+imageWidth*0;
var row2 = 11;
var b = canvasWidth/2-(imageWidth)*2/2+imageWidth*0;
var c = canvasWidth/2-(imageWidth)*2/2+imageWidth*1;
var row3 = 21
var d = canvasWidth/2-(imageWidth)*3/2+imageWidth*0;
var e = canvasWidth/2-(imageWidth)*3/2+imageWidth*1;
var f = canvasWidth/2-(imageWidth)*3/2+imageWidth*2;
var row4 = 31
var g = canvasWidth/2-(imageWidth)*4/2+imageWidth*0;
var h = canvasWidth/2-(imageWidth)*4/2+imageWidth*1;
var i = canvasWidth/2-(imageWidth)*4/2+imageWidth*2;
var j = canvasWidth/2-(imageWidth)*4/2+imageWidth*3;
var row5 = 41
var k = canvasWidth/2-(imageWidth)*5/2+imageWidth*0;
var m = canvasWidth/2-(imageWidth)*5/2+imageWidth*1;
var n = canvasWidth/2-(imageWidth)*5/2+imageWidth*2;
var o = canvasWidth/2-(imageWidth)*5/2+imageWidth*3;
var p = canvasWidth/2-(imageWidth)*5/2+imageWidth*4;

console.log("Row1: "+a +
            " Row2: "+ b + "," + c+
            " Row3: "+ d + "," + e+ ","+ f +
            " Row4: "+ g + "," + h +","+ i + "," + j +
            " Row5: "+ k + "," + m +","+ n + "," + o + "," + p);

To make correct loop you should understand what you Iterate, here, you are iterating rows and cells (bricks may be correct name), so to make a loop you should just make loop in loop, like in table: 要进行正确的循环,您应该了解要进行的迭代,在这里,您要对行和单元格进行迭代(砖可能是正确的名称),因此要进行循环,您应该像表中那样进行循环:

 var canvasWidth = 1000; var imageWidth = 20; var rows = []; for (var row = 1; row <= 5; row++) { // use let in es6 var cells = []; // use let in es6 rows[row - 1] = cells; for (var cell = 0; cell <= row - 1; cell++) { // use let in es6 cells[cell] = canvasWidth / 2 - (imageWidth) * row / 2 + imageWidth * cell; } } console.log(rows); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM