JavaScript - problems with multiindexing arrays and resetting arrays to draw disjoint curves












0















I'm trying to write JavaScript code to draw curves on a canvas. With some help I was able to get the code to draw curves (see Event handlers not responding) but I've been having problems with my attempts to generate disjoint curves.



My first attempt was to use multi-indexed arrays, with each subarray containing the points that make up each individual curve. Because I couldn't figure out what was going wrong, I switched to simply resetting the array.



In the code below, deleting




// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;


in the function mouseupfx allows my to draw as I should be able to. Why does that piece of code cause it to malfunction?




<script src="https://d3js.org/d3.v5.min.js"></script>
<script>

document.body.style.backgroundColor = "black";

canvw = 500;
canvh = 500;
border = 6;

clickDown = 0;
pointIndex = 0;
xin = ;
yin = ;

canvas = d3.select("body")
.append("svg") //Add an SVG to the body
.attr("width",canvw+2*border)
.attr("height",canvh+2*border)

canvbg = canvas.append("rect")
.attr("x",border)
.attr("y",border)
.attr("width",canvw)
.attr("height",canvh)
.attr("fill","white")
.attr("stroke","rgb(200,200,200)")
.attr("stroke-width",5)

mydiv = d3.select("body")
.append("div")
.attr("id","outputdiv")
.style("color","orange") //This is actually how I change the text color

canvas.on("mousedown", mousedownfx);

canvas.on("mouseup", mouseupfx);

canvas.on("mousemove",mousemovefx);

canvas.on("mouseout",mouseoutfx);

function mousedownfx() {
clickDown = 1;
/* Add an element ot xin and yin so I don't have to check every time the mouse moves
whether or not there's a previous entry in xin and yin so I can draw a line from it */
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
}

function mouseupfx() {
clickDown = 0;
// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;
}

function mouseoutfx() {
clickDown = 0;
}

function mousemovefx() {
if (clickDown == 1) {
// Add new point to curve data
pointIndex = pointIndex + 1;
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
// Draw new line segment
canvas.append("line")
.attr("x1",xin[pointIndex-1])
.attr("x2",xin[pointIndex])
.attr("y1",yin[pointIndex-1])
.attr("y2",yin[pointIndex])
.style("stroke","black")
.style("stroke-width",3);
}
}

</script>











share|improve this question























  • cause it to malfunction? Perhaps because the canvas is re-drawn in it's entirety each time and making the arrays empty essentially causes "blank" to be drawn.

    – radarbob
    Nov 21 '18 at 0:20
















0















I'm trying to write JavaScript code to draw curves on a canvas. With some help I was able to get the code to draw curves (see Event handlers not responding) but I've been having problems with my attempts to generate disjoint curves.



My first attempt was to use multi-indexed arrays, with each subarray containing the points that make up each individual curve. Because I couldn't figure out what was going wrong, I switched to simply resetting the array.



In the code below, deleting




// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;


in the function mouseupfx allows my to draw as I should be able to. Why does that piece of code cause it to malfunction?




<script src="https://d3js.org/d3.v5.min.js"></script>
<script>

document.body.style.backgroundColor = "black";

canvw = 500;
canvh = 500;
border = 6;

clickDown = 0;
pointIndex = 0;
xin = ;
yin = ;

canvas = d3.select("body")
.append("svg") //Add an SVG to the body
.attr("width",canvw+2*border)
.attr("height",canvh+2*border)

canvbg = canvas.append("rect")
.attr("x",border)
.attr("y",border)
.attr("width",canvw)
.attr("height",canvh)
.attr("fill","white")
.attr("stroke","rgb(200,200,200)")
.attr("stroke-width",5)

mydiv = d3.select("body")
.append("div")
.attr("id","outputdiv")
.style("color","orange") //This is actually how I change the text color

canvas.on("mousedown", mousedownfx);

canvas.on("mouseup", mouseupfx);

canvas.on("mousemove",mousemovefx);

canvas.on("mouseout",mouseoutfx);

function mousedownfx() {
clickDown = 1;
/* Add an element ot xin and yin so I don't have to check every time the mouse moves
whether or not there's a previous entry in xin and yin so I can draw a line from it */
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
}

function mouseupfx() {
clickDown = 0;
// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;
}

function mouseoutfx() {
clickDown = 0;
}

function mousemovefx() {
if (clickDown == 1) {
// Add new point to curve data
pointIndex = pointIndex + 1;
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
// Draw new line segment
canvas.append("line")
.attr("x1",xin[pointIndex-1])
.attr("x2",xin[pointIndex])
.attr("y1",yin[pointIndex-1])
.attr("y2",yin[pointIndex])
.style("stroke","black")
.style("stroke-width",3);
}
}

</script>











share|improve this question























  • cause it to malfunction? Perhaps because the canvas is re-drawn in it's entirety each time and making the arrays empty essentially causes "blank" to be drawn.

    – radarbob
    Nov 21 '18 at 0:20














0












0








0








I'm trying to write JavaScript code to draw curves on a canvas. With some help I was able to get the code to draw curves (see Event handlers not responding) but I've been having problems with my attempts to generate disjoint curves.



My first attempt was to use multi-indexed arrays, with each subarray containing the points that make up each individual curve. Because I couldn't figure out what was going wrong, I switched to simply resetting the array.



In the code below, deleting




// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;


in the function mouseupfx allows my to draw as I should be able to. Why does that piece of code cause it to malfunction?




<script src="https://d3js.org/d3.v5.min.js"></script>
<script>

document.body.style.backgroundColor = "black";

canvw = 500;
canvh = 500;
border = 6;

clickDown = 0;
pointIndex = 0;
xin = ;
yin = ;

canvas = d3.select("body")
.append("svg") //Add an SVG to the body
.attr("width",canvw+2*border)
.attr("height",canvh+2*border)

canvbg = canvas.append("rect")
.attr("x",border)
.attr("y",border)
.attr("width",canvw)
.attr("height",canvh)
.attr("fill","white")
.attr("stroke","rgb(200,200,200)")
.attr("stroke-width",5)

mydiv = d3.select("body")
.append("div")
.attr("id","outputdiv")
.style("color","orange") //This is actually how I change the text color

canvas.on("mousedown", mousedownfx);

canvas.on("mouseup", mouseupfx);

canvas.on("mousemove",mousemovefx);

canvas.on("mouseout",mouseoutfx);

function mousedownfx() {
clickDown = 1;
/* Add an element ot xin and yin so I don't have to check every time the mouse moves
whether or not there's a previous entry in xin and yin so I can draw a line from it */
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
}

function mouseupfx() {
clickDown = 0;
// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;
}

function mouseoutfx() {
clickDown = 0;
}

function mousemovefx() {
if (clickDown == 1) {
// Add new point to curve data
pointIndex = pointIndex + 1;
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
// Draw new line segment
canvas.append("line")
.attr("x1",xin[pointIndex-1])
.attr("x2",xin[pointIndex])
.attr("y1",yin[pointIndex-1])
.attr("y2",yin[pointIndex])
.style("stroke","black")
.style("stroke-width",3);
}
}

</script>











share|improve this question














I'm trying to write JavaScript code to draw curves on a canvas. With some help I was able to get the code to draw curves (see Event handlers not responding) but I've been having problems with my attempts to generate disjoint curves.



My first attempt was to use multi-indexed arrays, with each subarray containing the points that make up each individual curve. Because I couldn't figure out what was going wrong, I switched to simply resetting the array.



In the code below, deleting




// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;


in the function mouseupfx allows my to draw as I should be able to. Why does that piece of code cause it to malfunction?




<script src="https://d3js.org/d3.v5.min.js"></script>
<script>

document.body.style.backgroundColor = "black";

canvw = 500;
canvh = 500;
border = 6;

clickDown = 0;
pointIndex = 0;
xin = ;
yin = ;

canvas = d3.select("body")
.append("svg") //Add an SVG to the body
.attr("width",canvw+2*border)
.attr("height",canvh+2*border)

canvbg = canvas.append("rect")
.attr("x",border)
.attr("y",border)
.attr("width",canvw)
.attr("height",canvh)
.attr("fill","white")
.attr("stroke","rgb(200,200,200)")
.attr("stroke-width",5)

mydiv = d3.select("body")
.append("div")
.attr("id","outputdiv")
.style("color","orange") //This is actually how I change the text color

canvas.on("mousedown", mousedownfx);

canvas.on("mouseup", mouseupfx);

canvas.on("mousemove",mousemovefx);

canvas.on("mouseout",mouseoutfx);

function mousedownfx() {
clickDown = 1;
/* Add an element ot xin and yin so I don't have to check every time the mouse moves
whether or not there's a previous entry in xin and yin so I can draw a line from it */
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
}

function mouseupfx() {
clickDown = 0;
// Reset xin and yin since I was having a bear of a time trying to use multi-indexed arrays
xin = ;
yin = ;
}

function mouseoutfx() {
clickDown = 0;
}

function mousemovefx() {
if (clickDown == 1) {
// Add new point to curve data
pointIndex = pointIndex + 1;
xin.push(d3.mouse(this)[0]);
yin.push(d3.mouse(this)[1]);
// Draw new line segment
canvas.append("line")
.attr("x1",xin[pointIndex-1])
.attr("x2",xin[pointIndex])
.attr("y1",yin[pointIndex-1])
.attr("y2",yin[pointIndex])
.style("stroke","black")
.style("stroke-width",3);
}
}

</script>








javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 23:50









BGreenBGreen

1195




1195













  • cause it to malfunction? Perhaps because the canvas is re-drawn in it's entirety each time and making the arrays empty essentially causes "blank" to be drawn.

    – radarbob
    Nov 21 '18 at 0:20



















  • cause it to malfunction? Perhaps because the canvas is re-drawn in it's entirety each time and making the arrays empty essentially causes "blank" to be drawn.

    – radarbob
    Nov 21 '18 at 0:20

















cause it to malfunction? Perhaps because the canvas is re-drawn in it's entirety each time and making the arrays empty essentially causes "blank" to be drawn.

– radarbob
Nov 21 '18 at 0:20





cause it to malfunction? Perhaps because the canvas is re-drawn in it's entirety each time and making the arrays empty essentially causes "blank" to be drawn.

– radarbob
Nov 21 '18 at 0:20












0






active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403335%2fjavascript-problems-with-multiindexing-arrays-and-resetting-arrays-to-draw-dis%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403335%2fjavascript-problems-with-multiindexing-arrays-and-resetting-arrays-to-draw-dis%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)