<html>
<head>
<meta charset=utf-8 />
<title>Draw a rectangular shape</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<p>Drag to draw lines</p>
<canvas id="canvas" width=900 height=900></canvas>
<br/>
<button id="clear">Clear Canvas</button>
<br><br>
<button onclick="spitoutcode();" type="button">Spitout code!</button>
<div id="listes"></div>
<div id="listes2"></div>
<div id="new">
<!--
<p id="p1">Tutorix</p>
<p id="p2">Tutorialspoint</p>
-->
</div>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var canvasOffset = $("#canvas").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var storedLines = [];
var startX = 0;
var startY = 0;
var isDown;
console.log("stored", storedLines);
//let counter = 0;
//for (let i = 0; i < storage.length; i++) {
//if (storage[i].status === '0') counter++;
//}
//var animals = [];
//setInterval(function (){
//console.log(storedLines.lenght);
//console.log(storedLines);
//}, 300);
//for(i=0; i<storedLines.lenght; i++){
//const kopa = animals.push(storedLines[i]);
//console.log("zbisj", kopa);
//}
//console.log(storedLines);
var x = [];
console.log(x);
ctx.strokeStyle = "orange";
ctx.lineWidth = 3;
$("#canvas").mousedown(function(e) {
handleMouseDown(e);
});
$("#canvas").mousemove(function(e) {
handleMouseMove(e);
});
$("#canvas").mouseup(function(e) {
handleMouseUp(e);
});
$("#clear").click(function() {
storedLines.length = 0;
redrawStoredLines();
});
function handleMouseDown(e) {
var mouseX = parseInt(e.clientX - offsetX);
var mouseY = parseInt(e.clientY - offsetY);
isDown = true;
const lastItem = storedLines.slice(-1).pop();
var count = storedLines. length;
if(lastItem){
if(count > 0){
startX = lastItem.x2;
startY = lastItem.y2;
} else {
startX = mouseX;
startY = mouseY;
}
} else {
startX = mouseX;
startY = mouseY;
}
console.log("x", startX);
console.log("y", startY);
}
function handleMouseMove(e) {
if (!isDown) {
return;
}
redrawStoredLines();
var mouseX = parseInt(e.clientX - offsetX);
var mouseY = parseInt(e.clientY - offsetY);
// draw the current line
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(mouseX, mouseY);
ctx.stroke();
}
var list = [];
var list2 = [];
function handleMouseUp(e) {
isDown = false;
var mouseX = parseInt(e.clientX - offsetX);
var mouseY = parseInt(e.clientY - offsetY);
storedLines.push({
x1: startX,
y1: startY,
x2: mouseX,
y2: mouseY
});
for (var i = startX; i <= mouseX; i++) {
list.push(i);
}
for (var i = startY; i <= mouseY; i++) {
list2.push(i);
}
redrawStoredLines();
}
console.log("list" , list);
console.log("list2" , list2);
function spitoutcode(){
//foreach line create numbers in array, for every array element create html element and add content to it.
var len= storedLines. length;
//var list = storedLines;
//var test = 0;
//creates html elements
for(i2=0; i2 < 71; i2++){
if(i2 != 0){
tit2 = "p" + i2;
} else {
tit2 = "p";
}
//const div = document.createElement('listes');
//const div2 = document.createElement('listes2');
//div.className = 'row';
//div2.className = 'row';
//skaitļus no 1 līdz 6 pēc tam no 6 līdz 12 un so on
//div = '<p id="' + tit + '"></p>';
//div2 = '<p id="' + tit2 + '"></p>';
//document.getElementById('listes').appendChild(div);
//document.getElementById('listes2').appendChild(div2);
//var tag = document.createElement("p");
//tag.setAttribute("id", tit);
var tag2 = document.createElement("p");
tag2.setAttribute("id", tit2);
var element = document.getElementById("new");
//element.appendChild(tag);
element.appendChild(tag2);
//var test++;
}
for(i2=0; i2 < 25; i2++){
if(i2 != 0){
tit = "list" + i2;
} else {
tit = "list";
}
//const div = document.createElement('listes');
//const div2 = document.createElement('listes2');
//div.className = 'row';
//div2.className = 'row';
//skaitļus no 1 līdz 6 pēc tam no 6 līdz 12 un so on
//div = '<p id="' + tit + '"></p>';
//div2 = '<p id="' + tit2 + '"></p>';
//document.getElementById('listes').appendChild(div);
//document.getElementById('listes2').appendChild(div2);
var tag = document.createElement("p");
tag.setAttribute("id", tit);
//var tag2 = document.createElement("p");
//tag2.setAttribute("id", tit2);
var element = document.getElementById("new");
element.appendChild(tag);
//element.appendChild(tag2);
//var test++;
}
//creates html elements
//if(test > 0)
document.getElementById("p" + 1).innerHTML = " //var nTufts = 15000";
document.getElementById("p" + 2).innerHTML = " ///var positions = new Array(nTufts) ";
document.getElementById("p" + 3).innerHTML = " //for(var i = 0; i < nTufts; i++){";
document.getElementById("p" + 4).innerHTML = " //var position = new THREE.Vector3()";
document.getElementById("p" + 5).innerHTML = " //position.x = (Math.random()-0.5)*20";
document.getElementById("p" + 6).innerHTML = " //position.z = (Math.random()-0.5)*20";
document.getElementById("p" + 7).innerHTML = " //if(list.includes(position.x) or list2.includes(position.y)){";
document.getElementById("p" + 8).innerHTML = " //positions[i] = position";
document.getElementById("p" + 9).innerHTML = " //};";
document.getElementById("p" + 10).innerHTML = " //}";
document.getElementById("p" + 11).innerHTML = " //}";
document.getElementById("p" + 12).innerHTML = " //var mesh = THREEx.createGrassTufts(positions)";
document.getElementById("p" + 13).innerHTML = " //scene.add(mesh)";
// load the texture
document.getElementById("p" + 14).innerHTML = "var textureUrl = THREEx.createGrassTufts.baseUrl+'images/grass01.png'";
document.getElementById("p" + 15).innerHTML = "var material = mesh.material";
document.getElementById("p" + 16).innerHTML = "material.map = THREE.ImageUtils.loadTexture(textureUrl)";
document.getElementById("p" + 17).innerHTML = "material.alphaTest = 0.7";
document.getElementById("p" + 18).innerHTML = " //var nTufts = 15000";
document.getElementById("p" + 19).innerHTML = " ///var positions = new Array(nTufts) ";
document.getElementById("p" + 20).innerHTML = " //for(var i = 0; i < nTufts; i++){";
document.getElementById("p" + 21).innerHTML = " //var position = new THREE.Vector3()";
document.getElementById("p" + 22).innerHTML = " //position.x = (Math.random()-0.5)*20";
document.getElementById("p" + 23).innerHTML = " //position.z = (Math.random()-0.5)*20";
document.getElementById("p" + 24).innerHTML = " //if(list.includes(position.x) or list2.includes(position.y)){";
document.getElementById("p" + 25).innerHTML = " //positions[i] = position";
document.getElementById("p" + 26).innerHTML = " //};";
document.getElementById("p" + 27).innerHTML = " //}";
document.getElementById("p" + 28).innerHTML = " //}";
document.getElementById("p" + 29).innerHTML = " //var mesh = THREEx.createGrassTufts(positions)";
document.getElementById("p" + 30).innerHTML = " //scene.add(mesh)";
// load the texture
document.getElementById("p" + 31).innerHTML = "var textureUrl = THREEx.createGrassTufts.baseUrl+'images/grass02.png'";
document.getElementById("p" + 32).innerHTML = "var material = mesh.material";
document.getElementById("p" + 33).innerHTML = "material.map = THREE.ImageUtils.loadTexture(textureUrl)";
document.getElementById("p" + 34).innerHTML = "material.alphaTest = 0.7";
document.getElementById("p" + 35).innerHTML = " //var nTufts = 15000";
document.getElementById("p" + 36).innerHTML = " ///var positions = new Array(nTufts) ";
document.getElementById("p" + 37).innerHTML = " //for(var i = 0; i < nTufts; i++){";
document.getElementById("p" + 38).innerHTML = " //var position = new THREE.Vector3()";
document.getElementById("p" + 39).innerHTML = " //position.x = (Math.random()-0.5)*20";
document.getElementById("p" + 40).innerHTML = " //position.z = (Math.random()-0.5)*20";
document.getElementById("p" + 41).innerHTML = " //if(list.includes(position.x) or list2.includes(position.y)){";
document.getElementById("p" + 42).innerHTML = " //positions[i] = position";
document.getElementById("p" + 43).innerHTML = " //};";
document.getElementById("p" + 44).innerHTML = " //}";
document.getElementById("p" + 45).innerHTML = " //}";
document.getElementById("p" + 46).innerHTML = " //var mesh = THREEx.createGrassTufts(positions)";
document.getElementById("p" + 47).innerHTML = " //scene.add(mesh)";
// load the texture
document.getElementById("p" + 48).innerHTML = "var material = mesh.material";
document.getElementById("p" + 49).innerHTML = "var textureUrl = THREEx.createGrassTufts.baseUrl+'images/flowers01.png'";
document.getElementById("p" + 50).innerHTML = "material.map = THREE.ImageUtils.loadTexture(textureUrl)";
document.getElementById("p" + 51).innerHTML = "material.emissive.set(0x888888)";
document.getElementById("p" + 52).innerHTML = "material.alphaTest = 0.7";
document.getElementById("p" + 53).innerHTML = " //var nTufts = 15000";
document.getElementById("p" + 54).innerHTML = " ///var positions = new Array(nTufts) ";
document.getElementById("p" + 55).innerHTML = " //for(var i = 0; i < nTufts; i++){";
document.getElementById("p" + 56).innerHTML = " //var position = new THREE.Vector3()";
document.getElementById("p" + 57).innerHTML = " //position.x = (Math.random()-0.5)*20";
document.getElementById("p" + 58).innerHTML = " //position.z = (Math.random()-0.5)*20";
document.getElementById("p" + 59).innerHTML = " //if(list.includes(position.x) or list2.includes(position.y)){";
document.getElementById("p" + 60).innerHTML = " //positions[i] = position";
document.getElementById("p" + 61).innerHTML = " //};";
document.getElementById("p" + 62).innerHTML = " //}";
document.getElementById("p" + 63).innerHTML = " //}";
document.getElementById("p" + 64).innerHTML = " //var mesh = THREEx.createGrassTufts(positions)";
document.getElementById("p" + 65).innerHTML = " //scene.add(mesh)";
// load the texture
// load the texture
document.getElementById("p" + 66).innerHTML = "var material = mesh.material";
document.getElementById("p" + 67).innerHTML = "var textureUrl = THREEx.createGrassTufts.baseUrl+'images/flowers02.png'";
document.getElementById("p" + 68).innerHTML = "material.map = THREE.ImageUtils.loadTexture(textureUrl)";
document.getElementById("p" + 69).innerHTML = "material.emissive.set(0x888888)";
document.getElementById("p" + 70).innerHTML = "material.alphaTest = 0.7";
document.getElementById("list" + 1).innerHTML = " // var list =[";
document.getElementById("list" + 2).innerHTML = list;
document.getElementById("list" + 3).innerHTML = "];";
document.getElementById("list" + 4).innerHTML = " // var list =[";
document.getElementById("list" + 5).innerHTML = list2;
document.getElementById("list" + 6).innerHTML = "];";
document.getElementById("list" + 7).innerHTML = " // var list =[";
document.getElementById("list" + 8).innerHTML = list;
document.getElementById("list" + 9).innerHTML = "];";
document.getElementById("list" + 10).innerHTML = " // var list =[";
document.getElementById("list" + 11).innerHTML = list2;
document.getElementById("list" + 12).innerHTML = "];";
document.getElementById("list" + 13).innerHTML = " // var list =[";
document.getElementById("list" + 14).innerHTML = list;
document.getElementById("list" + 15).innerHTML = "];";
document.getElementById("list" + 16).innerHTML = " // var list =[";
document.getElementById("list" + 17).innerHTML = list2;
document.getElementById("list" + 18).innerHTML = "];";
document.getElementById("list" + 19).innerHTML = " // var list =[";
document.getElementById("list" + 20).innerHTML = list;
document.getElementById("list" + 21).innerHTML = "];";
document.getElementById("list" + 22).innerHTML = " // var list =[";
document.getElementById("list" + 23).innerHTML = list2;
document.getElementById("list" + 24).innerHTML = "];";
}
//spitoutcode();
function redrawStoredLines() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (storedLines.length == 0) {
return;
}
// redraw each stored line
for (var i = 0; i < storedLines.length; i++) {
ctx.beginPath();
ctx.moveTo(storedLines[i].x1, storedLines[i].y1);
ctx.lineTo(storedLines[i].x2, storedLines[i].y2);
ctx.stroke();
}
}
</script>
</body>
</html>