Canvas Infinite Scroll mapping object location
Trying to get infinite looping scrolling to work in KonvaJS, so there would be a grid of say 24 items, which would just keep scrolling over and over.
Thanks to @lavrton the basic grid is working, but adding items means that they don't stay in place on the redraw. Im guessing it relates to:
fill: grid[indexX][indexY],
Any ideas how I can map the text to the blocks?
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [
{ w: 150, h: 150 , background: "white" , image: "/img/test2.png" , fullImage: false, title: "" , text: "" },
{ w: 150, h: 150 , background: "white" , image: "/img/person-icon.png" , fullImage: false ,title: "" , text: "" },
{ w: 150, h: 150 , background: "#575756" , image: "" , fullImage: false, title: "Title" , text: "" },
{ w: 300, h: 300 , background: "white" , image: "/img/test.png", fullImage: true, title: "" , text: "" }
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
var i = 0;
for(var x = startX; x < endX; x += WIDTH) {
for(var y = startY; y < endY; y += HEIGHT) {
if(i === 4)
{
i = 0;
}
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if(blocks[i].title != ""){
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align : "center"
});
layer.add(complexText);
}
}
i++
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
https://jsfiddle.net/kiksy/jqo2h3dx/2/
javascript html5 canvas html5-canvas konvajs
add a comment |
Trying to get infinite looping scrolling to work in KonvaJS, so there would be a grid of say 24 items, which would just keep scrolling over and over.
Thanks to @lavrton the basic grid is working, but adding items means that they don't stay in place on the redraw. Im guessing it relates to:
fill: grid[indexX][indexY],
Any ideas how I can map the text to the blocks?
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [
{ w: 150, h: 150 , background: "white" , image: "/img/test2.png" , fullImage: false, title: "" , text: "" },
{ w: 150, h: 150 , background: "white" , image: "/img/person-icon.png" , fullImage: false ,title: "" , text: "" },
{ w: 150, h: 150 , background: "#575756" , image: "" , fullImage: false, title: "Title" , text: "" },
{ w: 300, h: 300 , background: "white" , image: "/img/test.png", fullImage: true, title: "" , text: "" }
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
var i = 0;
for(var x = startX; x < endX; x += WIDTH) {
for(var y = startY; y < endY; y += HEIGHT) {
if(i === 4)
{
i = 0;
}
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if(blocks[i].title != ""){
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align : "center"
});
layer.add(complexText);
}
}
i++
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
https://jsfiddle.net/kiksy/jqo2h3dx/2/
javascript html5 canvas html5-canvas konvajs
You are making illusion of infinite scroll. You needworld
andcamera
to make it work. Right know you are showing stuff at coordination that depends on the size ofcanvas
so whatever is outside won't work. It's easier to use something else than KonvaJS in this situation.
– Oen44
Jan 2 at 15:55
add a comment |
Trying to get infinite looping scrolling to work in KonvaJS, so there would be a grid of say 24 items, which would just keep scrolling over and over.
Thanks to @lavrton the basic grid is working, but adding items means that they don't stay in place on the redraw. Im guessing it relates to:
fill: grid[indexX][indexY],
Any ideas how I can map the text to the blocks?
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [
{ w: 150, h: 150 , background: "white" , image: "/img/test2.png" , fullImage: false, title: "" , text: "" },
{ w: 150, h: 150 , background: "white" , image: "/img/person-icon.png" , fullImage: false ,title: "" , text: "" },
{ w: 150, h: 150 , background: "#575756" , image: "" , fullImage: false, title: "Title" , text: "" },
{ w: 300, h: 300 , background: "white" , image: "/img/test.png", fullImage: true, title: "" , text: "" }
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
var i = 0;
for(var x = startX; x < endX; x += WIDTH) {
for(var y = startY; y < endY; y += HEIGHT) {
if(i === 4)
{
i = 0;
}
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if(blocks[i].title != ""){
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align : "center"
});
layer.add(complexText);
}
}
i++
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
https://jsfiddle.net/kiksy/jqo2h3dx/2/
javascript html5 canvas html5-canvas konvajs
Trying to get infinite looping scrolling to work in KonvaJS, so there would be a grid of say 24 items, which would just keep scrolling over and over.
Thanks to @lavrton the basic grid is working, but adding items means that they don't stay in place on the redraw. Im guessing it relates to:
fill: grid[indexX][indexY],
Any ideas how I can map the text to the blocks?
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [
{ w: 150, h: 150 , background: "white" , image: "/img/test2.png" , fullImage: false, title: "" , text: "" },
{ w: 150, h: 150 , background: "white" , image: "/img/person-icon.png" , fullImage: false ,title: "" , text: "" },
{ w: 150, h: 150 , background: "#575756" , image: "" , fullImage: false, title: "Title" , text: "" },
{ w: 300, h: 300 , background: "white" , image: "/img/test.png", fullImage: true, title: "" , text: "" }
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
var i = 0;
for(var x = startX; x < endX; x += WIDTH) {
for(var y = startY; y < endY; y += HEIGHT) {
if(i === 4)
{
i = 0;
}
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if(blocks[i].title != ""){
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align : "center"
});
layer.add(complexText);
}
}
i++
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
https://jsfiddle.net/kiksy/jqo2h3dx/2/
javascript html5 canvas html5-canvas konvajs
javascript html5 canvas html5-canvas konvajs
edited Dec 19 '18 at 17:49


chazsolo
5,4001234
5,4001234
asked Dec 19 '18 at 17:31
KiksyKiksy
31821433
31821433
You are making illusion of infinite scroll. You needworld
andcamera
to make it work. Right know you are showing stuff at coordination that depends on the size ofcanvas
so whatever is outside won't work. It's easier to use something else than KonvaJS in this situation.
– Oen44
Jan 2 at 15:55
add a comment |
You are making illusion of infinite scroll. You needworld
andcamera
to make it work. Right know you are showing stuff at coordination that depends on the size ofcanvas
so whatever is outside won't work. It's easier to use something else than KonvaJS in this situation.
– Oen44
Jan 2 at 15:55
You are making illusion of infinite scroll. You need
world
and camera
to make it work. Right know you are showing stuff at coordination that depends on the size of canvas
so whatever is outside won't work. It's easier to use something else than KonvaJS in this situation.– Oen44
Jan 2 at 15:55
You are making illusion of infinite scroll. You need
world
and camera
to make it work. Right know you are showing stuff at coordination that depends on the size of canvas
so whatever is outside won't work. It's easier to use something else than KonvaJS in this situation.– Oen44
Jan 2 at 15:55
add a comment |
1 Answer
1
active
oldest
votes
Explanation:
The issue is the calculation of i
. It needs to be based on the values of indexX
and indexY
Something like this:
//maps from 0 to 3
const i = indexX * 2 + indexY;
When observing your blocks array, only one has title and it is at the index of 2
which (depending on your perspective) corresponds to the green color.
const i = 1 * 2 + 0; //index 2
Why * 2
? It's just hard coded, but corresponds to the inner grid array length which is 2.
(Ex: grid[0].length)
Full solution:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Full Solution with Red and Blue having titles:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Mirror Issue Solution:
This is directly caused because of the following calculations:
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
and solved with:
const indexX = ((x/WIDTH) + grid.length * WIDTH ) % grid.length;
const indexY = ((y/HEIGHT) + grid.length * HEIGHT) % grid[0].length;
When you calculate colors you base it off of your grid array:
['red', 'yellow', 'pink' ]
['green', 'blue' , 'gray' ]
['orange', 'blue' , 'black']
And when you advance logically down or right (positive values) there are no issues, when you move up or left (negative values) everything goes nuts because you calculate indexX and indexY with Math.abs.
So for example, a red box located [-1,-1] doesn't match any color in the grid. Based on your formula, it'll simply choose the 'blue' color when in fact it should be the 'black' color.
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53856458%2fcanvas-infinite-scroll-mapping-object-location%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Explanation:
The issue is the calculation of i
. It needs to be based on the values of indexX
and indexY
Something like this:
//maps from 0 to 3
const i = indexX * 2 + indexY;
When observing your blocks array, only one has title and it is at the index of 2
which (depending on your perspective) corresponds to the green color.
const i = 1 * 2 + 0; //index 2
Why * 2
? It's just hard coded, but corresponds to the inner grid array length which is 2.
(Ex: grid[0].length)
Full solution:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Full Solution with Red and Blue having titles:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Mirror Issue Solution:
This is directly caused because of the following calculations:
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
and solved with:
const indexX = ((x/WIDTH) + grid.length * WIDTH ) % grid.length;
const indexY = ((y/HEIGHT) + grid.length * HEIGHT) % grid[0].length;
When you calculate colors you base it off of your grid array:
['red', 'yellow', 'pink' ]
['green', 'blue' , 'gray' ]
['orange', 'blue' , 'black']
And when you advance logically down or right (positive values) there are no issues, when you move up or left (negative values) everything goes nuts because you calculate indexX and indexY with Math.abs.
So for example, a red box located [-1,-1] doesn't match any color in the grid. Based on your formula, it'll simply choose the 'blue' color when in fact it should be the 'black' color.
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
add a comment |
Explanation:
The issue is the calculation of i
. It needs to be based on the values of indexX
and indexY
Something like this:
//maps from 0 to 3
const i = indexX * 2 + indexY;
When observing your blocks array, only one has title and it is at the index of 2
which (depending on your perspective) corresponds to the green color.
const i = 1 * 2 + 0; //index 2
Why * 2
? It's just hard coded, but corresponds to the inner grid array length which is 2.
(Ex: grid[0].length)
Full solution:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Full Solution with Red and Blue having titles:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Mirror Issue Solution:
This is directly caused because of the following calculations:
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
and solved with:
const indexX = ((x/WIDTH) + grid.length * WIDTH ) % grid.length;
const indexY = ((y/HEIGHT) + grid.length * HEIGHT) % grid[0].length;
When you calculate colors you base it off of your grid array:
['red', 'yellow', 'pink' ]
['green', 'blue' , 'gray' ]
['orange', 'blue' , 'black']
And when you advance logically down or right (positive values) there are no issues, when you move up or left (negative values) everything goes nuts because you calculate indexX and indexY with Math.abs.
So for example, a red box located [-1,-1] doesn't match any color in the grid. Based on your formula, it'll simply choose the 'blue' color when in fact it should be the 'black' color.
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
add a comment |
Explanation:
The issue is the calculation of i
. It needs to be based on the values of indexX
and indexY
Something like this:
//maps from 0 to 3
const i = indexX * 2 + indexY;
When observing your blocks array, only one has title and it is at the index of 2
which (depending on your perspective) corresponds to the green color.
const i = 1 * 2 + 0; //index 2
Why * 2
? It's just hard coded, but corresponds to the inner grid array length which is 2.
(Ex: grid[0].length)
Full solution:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Full Solution with Red and Blue having titles:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Mirror Issue Solution:
This is directly caused because of the following calculations:
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
and solved with:
const indexX = ((x/WIDTH) + grid.length * WIDTH ) % grid.length;
const indexY = ((y/HEIGHT) + grid.length * HEIGHT) % grid[0].length;
When you calculate colors you base it off of your grid array:
['red', 'yellow', 'pink' ]
['green', 'blue' , 'gray' ]
['orange', 'blue' , 'black']
And when you advance logically down or right (positive values) there are no issues, when you move up or left (negative values) everything goes nuts because you calculate indexX and indexY with Math.abs.
So for example, a red box located [-1,-1] doesn't match any color in the grid. Based on your formula, it'll simply choose the 'blue' color when in fact it should be the 'black' color.
Explanation:
The issue is the calculation of i
. It needs to be based on the values of indexX
and indexY
Something like this:
//maps from 0 to 3
const i = indexX * 2 + indexY;
When observing your blocks array, only one has title and it is at the index of 2
which (depending on your perspective) corresponds to the green color.
const i = 1 * 2 + 0; //index 2
Why * 2
? It's just hard coded, but corresponds to the inner grid array length which is 2.
(Ex: grid[0].length)
Full solution:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Full Solution with Red and Blue having titles:
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
Mirror Issue Solution:
This is directly caused because of the following calculations:
const indexX = Math.abs(x / WIDTH) % grid.length;
const indexY = Math.abs(y / HEIGHT) % grid[0].length;
and solved with:
const indexX = ((x/WIDTH) + grid.length * WIDTH ) % grid.length;
const indexY = ((y/HEIGHT) + grid.length * HEIGHT) % grid[0].length;
When you calculate colors you base it off of your grid array:
['red', 'yellow', 'pink' ]
['green', 'blue' , 'gray' ]
['orange', 'blue' , 'black']
And when you advance logically down or right (positive values) there are no issues, when you move up or left (negative values) everything goes nuts because you calculate indexX and indexY with Math.abs.
So for example, a red box located [-1,-1] doesn't match any color in the grid. Based on your formula, it'll simply choose the 'blue' color when in fact it should be the 'black' color.
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "Title",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
const WIDTH = 100;
const HEIGHT = 100;
const grid = [
['red', 'yellow'],
['green', 'blue']
];
const blocks = [{
w: 150,
h: 150,
background: "white",
image: "/img/test2.png",
fullImage: false,
title: "Title",
text: ""
},
{
w: 150,
h: 150,
background: "white",
image: "/img/person-icon.png",
fullImage: false,
title: "",
text: ""
},
{
w: 150,
h: 150,
background: "#575756",
image: "",
fullImage: false,
title: "",
text: ""
},
{
w: 300,
h: 300,
background: "white",
image: "/img/test.png",
fullImage: true,
title: "Title",
text: ""
}
];
function checkShapes() {
const startX = Math.floor((-stage.x() - stage.width()) / WIDTH) * WIDTH;
const endX = Math.floor((-stage.x() + stage.width() * 2) / WIDTH) * WIDTH;
const startY = Math.floor((-stage.y() - stage.height()) / HEIGHT) * HEIGHT;
const endY = Math.floor((-stage.y() + stage.height() * 2) / HEIGHT) * HEIGHT;
for (var x = startX; x < endX; x += WIDTH) {
for (var y = startY; y < endY; y += HEIGHT) {
const indexX = ((x / WIDTH) + grid.length * WIDTH) % grid.length;
const indexY = ((y / HEIGHT) + grid[0].length * HEIGHT) % grid[0].length;
//maps from 0 to 3
const i = indexX * 2 + indexY;
layer.add(new Konva.Rect({
x,
y,
width: WIDTH,
height: HEIGHT,
fill: grid[indexX][indexY],
stroke: 'black',
strokeWidth: 4
}))
if (blocks[i].title != "") {
var complexText = new Konva.Text({
x,
y,
text: "TEST TEXT",
fontSize: 14,
fontFamily: 'Calibri',
fill: 'white',
width: WIDTH,
height: HEIGHT,
verticalAlign: 'middle',
align: "center"
});
layer.add(complexText);
}
}
}
}
checkShapes();
layer.draw();
stage.on('dragend', () => {
layer.destroyChildren();
checkShapes();
layer.draw();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.6.0/konva.min.js"></script>
<div id="container"></div>
edited Jan 3 at 18:27
answered Jan 2 at 17:12


kemicofakemicofa
10.7k44083
10.7k44083
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
add a comment |
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
Wow thanks! That answers my question. I've expanded on it here: jsfiddle.net/kiksy/hpduwr09/3 you can see that the 2 titled blocks stay in the correct order when dragging down or left, but draggin up or right means they go out of order. I assume becuase it needs to draw in reverse for a negative x/y drag?
– Kiksy
Jan 3 at 15:57
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
I think I understood that part, the issue is the redraw on a up or left drag (so a minus) seems to redraw in the wrong order, even with the updated * 2 .see jsfiddle.net/kiksy/hpduwr09/10 . Notice the 4 numbered blocks go out of order. Thanks for your help with this. Its been melting my brain for so long now.
– Kiksy
Jan 3 at 16:18
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
ah yes! I've just been playing around with trying to adjust those values based on the drag direction! Your solution is much simpler! Thanks so much.
– Kiksy
Jan 3 at 16:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53856458%2fcanvas-infinite-scroll-mapping-object-location%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You are making illusion of infinite scroll. You need
world
andcamera
to make it work. Right know you are showing stuff at coordination that depends on the size ofcanvas
so whatever is outside won't work. It's easier to use something else than KonvaJS in this situation.– Oen44
Jan 2 at 15:55