How Multiple Canvas - Same Interaction - html

I'm trying to use this interaction but on 3 canvas in a row. I've been trying to play around with it for some time and failed to add multiple canvas that use the same interaction without interfering with one another.
I thought of uniquely call them canvas1, canvas2 and canvas3, but I'm wondering if anyone knew a simpler way?
I made a rough hide/show jquery version of what I have in mind but instead, it would use that scratching interaction.
Thank you!
$(".image1").click(function(){
$(".image1").hide();
$("p").show();
});
$(".image2").click(function(){
$(".image2").hide();
$("p").show();
});
$(".image3").click(function(){
$(".image3").hide();
$("p").show();
});
.pannel{
width: calc(33% - 40px);
height: 220px;
float: left;
margin: 0 20px;
}
.image1, .image2, .image3{
max-width:100px !important;
background-size: contain;
background-repeat: no-repeat;
background-position: bottom;
position:absolute;
}
.text-div{
width: 80%;
text-align: center;
margin:auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row pannels">
<div class="col-lg-12">
<div class="pannels-div">
<div class="pannel ">
<div class="image1"><img src="https://www.patrickmorin.com/media/catalog/product/cache/e4d64343b1bc593f1c5348fe05efa4a6/a/s/asphalt-shingle-lifetime-1-BAREVGL-fr.jpg" width="220"></div>
<div class="text-div">
<p> Vestibulum tempor erat at mauris aliquet, eu laoreet mi imperdiet.</p>
</div>
</div>
<div class="pannel">
<div class="image2"><img src="https://www.patrickmorin.com/media/catalog/product/cache/e4d64343b1bc593f1c5348fe05efa4a6/a/s/asphalt-shingle-lifetime-1-BAREVGL-fr.jpg" width="220"></div>
<div class="text-div">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim risus et orci fringilla semper.</p>
</div>
</div>
<div class="pannel">
<div class="image3"><img src="https://www.patrickmorin.com/media/catalog/product/cache/e4d64343b1bc593f1c5348fe05efa4a6/a/s/asphalt-shingle-lifetime-1-BAREVGL-fr.jpg" width="220"></div>
<div class="text-div">
<p>Praesent eget congue sem, tempor fermentum augue. Duis vulputate, libero vitae efficitur convallis.</p>
</div>
</div>
</div>
</div>
</div>
</div>

Okay so I've worked it out by adding a number on each class and each canvas's. It would be nice to know if there's an optimal way to use the same drawing effect but for multiple canvas.
The code is a mess, but I guess it works now
(function () {
'use strict';
/* 1er */
var isDrawing1, lastPoint1;
var container1 = document.getElementById('js-container1');
var canvas1 = document.getElementById('js-canvas1');
var canvas1Width = canvas1.width;
var canvas1Height = canvas1.height;
var ctx1 = canvas1.getContext('2d');
var image1 = new Image();
var brush1 = new Image();
/* 2e */
var isDrawing2, lastPoint2;
var container2 = document.getElementById('js-container2');
var canvas2 = document.getElementById('js-canvas2');
var canvas2Width = canvas2.width;
var canvas2Height = canvas2.height;
var ctx2 = canvas2.getContext('2d');
var image2 = new Image();
var brush2 = new Image();
/* 3e */
var isDrawing3, lastPoint3;
var container3 = document.getElementById('js-container3');
var canvas3 = document.getElementById('js-canvas3');
var canvas3Width = canvas3.width;
var canvas3Height = canvas3.height;
var ctx3 = canvas3.getContext('2d');
var image3 = new Image();
var brush3 = new Image();
/* Load top images */
image1.src = 'http://placehold.it/324x289/145f46?text=Canva1';
image2.src = 'http://placehold.it/324x289/145f46?text=Canva2';
image3.src = 'http://placehold.it/324x289/145f46?text=Canva3';
draw1();
draw2();
draw3();
function draw1() {
// image du dessus
image1.onload = function () {
ctx1.drawImage(image1, 0, 0);
// Show the scratchText when Image is loaded.
document.querySelectorAll('.scratchText1')[0].style.visibility = 'visible';
};
//image du brush a scratch
brush1.src = 'https://icon-library.com/images/3-dots-icon/3-dots-icon-9.jpg';
//Mouvement de la souris
canvas1.addEventListener('mousedown', handleMouseDown, false);
canvas1.addEventListener('touchstart', handleMouseDown, false);
canvas1.addEventListener('mousemove', handleMouseMove, false);
canvas1.addEventListener('touchmove', handleMouseMove, false);
canvas1.addEventListener('mouseup', handleMouseUp, false);
canvas1.addEventListener('touchend', handleMouseUp, false);
function distanceBetween(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
function angleBetween(point1, point2) {
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
}
function getMouse(e, canvas1) {
var offsetX = 0,
offsetY = 0,
mx, my;
if (canvas1.offsetParent !== undefined) {
do {
offsetX += canvas1.offsetLeft;
offsetY += canvas1.offsetTop;
} while ((canvas1 = canvas1.offsetParent));
}
mx = (e.pageX || e.touches[0].clientX) - offsetX;
my = (e.pageY || e.touches[0].clientY) - offsetY;
return {
x: mx,
y: my
};
}
function handleMouseDown(e) {
isDrawing1 = true;
lastPoint1 = getMouse(e, canvas1);
}
function handleMouseMove(e) {
if (!isDrawing1) {
return;
}
e.preventDefault();
var currentPoint = getMouse(e, canvas1),
dist = distanceBetween(lastPoint1, currentPoint),
angle = angleBetween(lastPoint1, currentPoint),
x, y;
for (var i = 0; i < dist; i++) {
x = lastPoint1.x + (Math.sin(angle) * i) - 25;
y = lastPoint1.y + (Math.cos(angle) * i) - 25;
ctx1.globalCompositeOperation = 'destination-out';
ctx1.drawImage(brush1, x, y);
}
lastPoint1 = currentPoint;
}
function handleMouseUp(e) {
isDrawing1 = false;
}
}
/* 2e */
function draw2() {
// image du dessus
image2.onload = function () {
ctx2.drawImage(image2, 0, 0);
// Show the scratchText when Image is loaded.
document.querySelectorAll('.scratchText2')[0].style.visibility = 'visible';
};
//image du brush a scratch
brush2.src = 'https://icon-library.com/images/3-dots-icon/3-dots-icon-9.jpg';
//Mouvement de la souris
canvas2.addEventListener('mousedown', handleMouseDown, false);
canvas2.addEventListener('touchstart', handleMouseDown, false);
canvas2.addEventListener('mousemove', handleMouseMove, false);
canvas2.addEventListener('touchmove', handleMouseMove, false);
canvas2.addEventListener('mouseup', handleMouseUp, false);
canvas2.addEventListener('touchend', handleMouseUp, false);
function distanceBetween(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
function angleBetween(point1, point2) {
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
}
function getMouse(e, canvas2) {
var offsetX = 0,
offsetY = 0,
mx, my;
if (canvas2.offsetParent !== undefined) {
do {
offsetX += canvas2.offsetLeft;
offsetY += canvas2.offsetTop;
} while ((canvas2 = canvas2.offsetParent));
}
mx = (e.pageX || e.touches[0].clientX) - offsetX;
my = (e.pageY || e.touches[0].clientY) - offsetY;
return {
x: mx,
y: my
};
}
function handleMouseDown(e) {
isDrawing2 = true;
lastPoint2 = getMouse(e, canvas2);
}
function handleMouseMove(e) {
if (!isDrawing2) {
return;
}
e.preventDefault();
var currentPoint = getMouse(e, canvas2),
dist = distanceBetween(lastPoint2, currentPoint),
angle = angleBetween(lastPoint2, currentPoint),
x, y;
for (var i = 0; i < dist; i++) {
x = lastPoint2.x + (Math.sin(angle) * i) - 25;
y = lastPoint2.y + (Math.cos(angle) * i) - 25;
ctx2.globalCompositeOperation = 'destination-out';
ctx2.drawImage(brush2, x, y);
}
lastPoint2 = currentPoint;
}
function handleMouseUp(e) {
isDrawing2 = false;
}
}
/* 3e */
function draw3() {
// image du dessus
image3.onload = function () {
ctx3.drawImage(image3, 0, 0);
// Show the scratchText when Image is loaded.
document.querySelectorAll('.scratchText3')[0].style.visibility = 'visible';
};
//image du brush a scratch
brush3.src = 'https://icon-library.com/images/3-dots-icon/3-dots-icon-9.jpg';
//Mouvement de la souris
canvas3.addEventListener('mousedown', handleMouseDown, false);
canvas3.addEventListener('touchstart', handleMouseDown, false);
canvas3.addEventListener('mousemove', handleMouseMove, false);
canvas3.addEventListener('touchmove', handleMouseMove, false);
canvas3.addEventListener('mouseup', handleMouseUp, false);
canvas3.addEventListener('touchend', handleMouseUp, false);
function distanceBetween(point1, point2) {
return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
}
function angleBetween(point1, point2) {
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
}
function getMouse(e, canvas3) {
var offsetX = 0,
offsetY = 0,
mx, my;
if (canvas3.offsetParent !== undefined) {
do {
offsetX += canvas3.offsetLeft;
offsetY += canvas3.offsetTop;
} while ((canvas3 = canvas3.offsetParent));
}
mx = (e.pageX || e.touches[0].clientX) - offsetX;
my = (e.pageY || e.touches[0].clientY) - offsetY;
return {
x: mx,
y: my
};
}
function handleMouseDown(e) {
isDrawing3 = true;
lastPoint3 = getMouse(e, canvas3);
}
function handleMouseMove(e) {
if (!isDrawing3) {
return;
}
e.preventDefault();
var currentPoint = getMouse(e, canvas3),
dist = distanceBetween(lastPoint3, currentPoint),
angle = angleBetween(lastPoint3, currentPoint),
x, y;
for (var i = 0; i < dist; i++) {
x = lastPoint3.x + (Math.sin(angle) * i) - 25;
y = lastPoint3.y + (Math.cos(angle) * i) - 25;
ctx3.globalCompositeOperation = 'destination-out';
ctx3.drawImage(brush3, x, y);
}
lastPoint3 = currentPoint;
}
function handleMouseUp(e) {
isDrawing3 = false;
}
}
})();
body {
padding: 20px 0;
}
.container1, .container2, .container3 {
border: 3px solid yellow;
position: relative;
width: 324px;
height: 289px;
margin: 0 auto;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
.canvas1 {
position: absolute;
top: 0;
}
.canvas2 {
position: absolute;
top: 0;
}
.canvas3 {
position: absolute;
top: 0;
}
.scratchText1 {
padding: 20px;
}
.scratchText2 {
padding: 20px;
}
.scratchText3 {
padding: 20px;
}
<div class="container">
<div class="container1">
<div id="js-container1">
<canvas class="canvas1" id="js-canvas1" width="324" height="289"></canvas>
<div class="scratchText1" style="visibility: hidden;">
<h2>'Allo, 'Allo!</h2>
<h3>The secret code is:</h3>
<h1><code>HlkafSYc</code></h1>
</div>
</div>
</div>
<div class="container2">
<div id="js-container2">
<canvas class="canvas2" id="js-canvas2" width="324" height="289"></canvas>
<div class="scratchText2" style="visibility: hidden;">
<h2>'Allo, 'Allo!</h2>
<h3>The secret code is:</h3>
<h1><code>HlkafSYc</code></h1>
</div>
</div>
</div>
<div class="container3">
<div id="js-container3">
<canvas class="canvas3" id="js-canvas3" width="324" height="289"></canvas>
<div class="scratchText3" style="visibility: hidden;">
<h2>'Allo, 'Allo!</h2>
<h3>The secret code is:</h3>
<h1><code>HlkafSYc</code></h1>
</div>
</div>
</div>
</div>

Related

Why the value of id="demo2: is still zero evene it is incrementing by one

So what I am trying to do is, animate using javascript. It is working fine but I also want to show the value of pos(variable) in code, to show the value gradually.
problem:
the value of demo2 is not changing even the scope of the variable is same. why not #demo2 not changing.
Please let me know the reason and solution that value of the id #demo2 also change as the value of id #demo changing.
<!DOCTYPE html>
<html>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
</style>
<body>
function myMove() {
let id = null;
const elem = document.getElementById("animate");
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
document.getElementById("demo").innerHTML = pos;
}
document.getElementById("demo2").innerHTML = pos;
}
</script>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
</style>
<p><button onclick="myMove()">Click Me</button></p>
<div id ="container">
<div id ="animate"></div>
</div>
<p id="demo"></p>
<p id="demo2"></p>
function myMove() {
let id = null;
const elem = document.getElementById("animate");
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
document.getElementById("demo").innerHTML = pos;
}
document.getElementById("demo2").innerHTML = pos;
}
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
<p><button onclick="myMove()">Click Me</button></p>
<div id ="container">
<div id ="animate"></div>
</div>
<p id="demo"></p>
<p id="demo2"></p>
<p><button onclick="myMove()">Click Me</button></p>
<div id="container">
<div id="animate"></div>
</div>
<p id="demo"></p>
<p id="demo2"></p>
<script>
function myMove() {
let id = null;
const elem = document.getElementById("animate");
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
document.getElementById("demo").innerHTML = pos;
}
document.getElementById("demo2").innerHTML = pos;
}
</script>
</body>
</html>
The function frame() is executed after every other instructions in your function myMove().
If you want to change the value of #demo2 you have to put it in the same scope as the function frame() because that is where the value is updated. Otherwise you will get the value pos has when it is initialized.
function myMove() {
let id = null;
const elem = document.getElementById("animate");
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
document.getElementById("demo").innerHTML = pos;
document.getElementById("demo2").innerHTML = pos;
}
}

table with height 100% doesn't fit parent size

I've a table inside a div inside the main.
all of this have width = 100% but when i make the window smaller just the div ant the main gets smaller but the table dosent resize. all other elements below the table change position and size and starts lay over it. the table has 25 records and if the window is full-size everything matches perfect.
The div im talkin about has the id home
The html:
<DOCTYPE! html>
<html>
<head>
<meta charset="UTF-8">
<title>Admin Panel | Please Login</title>
<link href='../css/admin.css' type='text/css' rel='stylesheet'>
<meta http-equiv="refresh" content="60">
</head>
<body>
<main>
<div id='container'>
<img src="../img/ipw_quer_rgb.jpg" alt="IPW Logo" id="logo" width="15%">
<header>
<ul id='menu'>
<div id="links">
<li>Home</li>
<li>Schüler verwalten</li>
<li>History</li>
</div>
</ul>
</header>
<div id="home">
<h2 id="date"></h2>
<table id="table">
</table>
</div>
<div id="dropdown" class="dropdown">
<select id="select">
<option value="Nothing">Nichts ausgewählt</option>
<option value="Extern">Extern</option>
<option value="Termin">Termin</option>
<option value="Schule">Schule</option>
</select>
</div>
<div id="legend" class="legend">
<svg width="10" height="10">
<rect x="0" y="0" width="10" height="10" style="fill:#D3D3D3;" />
</svg>
<a>Extern</a>
<br>
<svg width="10" height="10">
<rect x="0" y="0" width="10" height="10" style="fill:#FFAEB9;" />
</svg>
<a>Termin</a>
<br>
<svg width="10" height="10">
<rect x="0" y="0" width="10" height="10" style="fill:#FFFF00;" />
</svg>
<a>Schule</a>
<br>
<svg width="10" height="10">
<rect x="0" y="0" width="10" height="10" style="fill:#00FF00;" />
</svg>
<a>Visiert</a>
<br>
<button id="edit">Editieren</button>
</div>
</div>
</main>
the css:
*{
margin: 0;
padding: 0;
font-family: monospace;
box-sizing: border-box;
}
main{
height: 100%;
width: 100%;
}
label{
font: 13px Helvetica, Arial, sans-serif;
}
html, body{
background-color: #006975;
overflow-y:auto;
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
h3{
margin-right:50%;
}
table{
width:100%;
height:calc(100% -50px);
}
ul {
width:100%;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #006975;
}
li {
width:25%;
float: left;
}
h1{
text-align:center;
}
li a {
display: block;
color: white;
font-size: 120%;
text-align: center;
padding: 14px 16px;
border-left: 2px solid #fff;
border-right: 2px solid #fff;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
color: #006975;
background-color: #fff;
}
button:hover{
color:#fff;
background-color:#84afb8;
}
#container{
position:absolute;
margin: 4% 4% 4% 4%;
padding: 2%;
width: 90%;
height: 90%;
background-color: #fff;
}
#home{
height:60%;
}
#dropdown{
width:100%;
height:2%;
visibility:hidden;
}
.cell {
height: 4%;
width:10%;
text-align: center;
background-color: #D3D3D3;
}
.cell.on {
height: 4%;
width: 10%;
background-color: #00FF00;
}
.cell.les {
height: 4%;
width: 10%;
background-color: #FFFF00;
}
.cell.term {
height: 4%;
width: 10%;
background-color: #FFAEB9;
}
.cell.ext{
height: 4%;
width: 10%;
background-color: #D3D3D3;
}
.cell.spacer {
height: 4%;
width: 10%;
background-color:white;
}
.name {
border: 1px solid black;
}
if you also need the javascript please ask
EDIT:
javascript:
getDataUser('logGLAUSB');
var names = ["Zebra","Benj", "Nico", "Timon","Miro", "Leo"];
var longpresstimer = null;
getData();
window.addEventListener('click', function(){
});
window.addEventListener('load', function () {
var clickcount = 0;
var singleClickTimer;
document.getElementById('table').addEventListener('click', function (event) {
clickcount++;
if(clickcount==1){
if(event.target.tagName != "INPUT" && event.target.classList != 'cell spacer'){
singleClickTimer = setTimeout(function() {
clickcount = 0;
var cell = event.target;
var selected = getSelected();
if(selected == 3){
cell.classList.remove("ext");
cell.classList.remove("term");
cell.classList.remove("les");
cell.classList.add("on");
}else{
cell.classList.remove("ext");
cell.classList.remove("term");
cell.classList.remove("les");
cell.classList.remove("on");
switch(selected){
case 0: cell.classList.add("ext"); break;
case 1: cell.classList.add("les"); break;
case 2: cell.classList.add("term"); break;
}
}
var x = "get";
x += getString(event.target.parentNode.cells[0].childNodes[0].innerHTML);
getData(x);
}, 300);
}
}else if (clickcount == 2){
if(event.target.classList != "name"){
clearTimeout(singleClickTimer);
clickcount = 0;
toInput(event.target);
}
}
});
});
document.getElementById("edit").addEventListener('click', function(){
var legend = document.getElementById("legend");
var dropdown = document.getElementById('dropdown');
var select = document.getElementById('select');
legend.style.visibility = 'hidden';
dropdown.style.visibility = 'visible';
var button = document.createElement('button');
button.innerHTML= "Fertig";
dropdown.appendChild(button);
button.onclick = function(){
legend.style.visibility = 'visible';
dropdown.style.visibility = 'hidden';
dropdown.removeChild(button);
select.value = "Nothing";
}
});
function reset(){
var rows = Array.from(document.getElementsByClassName('row'));
var table = document.getElementById('table');
rows.forEach(function (row){
var cells = Array.from(document.getElementsByClassName('cell'));
for(var i = 0;i< cells.length;i++){
var cell = cells[i]
cell.classList.remove('on');
cell.classList.remove('les');
cell.classList.remove('term');
cell.classList.remove('ext');
}
});
var x = "rep";
x += getResetString();
getData(x);
}
function clearSelection() {
if(document.selection && document.selection.empty) {
document.selection.empty();
} else if(window.getSelection) {
var sel = window.getSelection();
sel.removeAllRanges();
}
}
function getData(str) {
var requestURL = "http://adopraesenz.ipwin.ch/data/students.php?q=" +str;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200){
loadJson(request);
}
};
request.open("GET", requestURL, true);
request.send();
}
function getDataHistory(str) {
var requestURL = "http://adopraesenz.ipwin.ch/data/history.php?q=" +str;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200){
if(request.responseText != ""){
loadDate(request);
}
}
};
request.open("GET", requestURL, true);
request.send();
}
function getDataUser(str){
var requestURL = "http://adopraesenz.ipwin.ch/data/login.php?q=" +str;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200){
if(request.responseText != ""){
loadDate(request);
} }
};
request.open("GET", requestURL, true);
request.send();
}
function getSelected(cell) {
var value = document.getElementById("select").value;
switch(value){
case "Extern": return 0; break;
case "Schule": return 1; break;
case "Termin": return 2; break;
default: return 3;
}
}
function loadDate(request){
var newDate = new Date();
newDate.setDate(newDate.getDate() -1);
newDate.setHours(0,0,0,0);
var days = request.responseText.split(".");
var oldDate = new Date(days[1]+"."+days[0]+"."+days[2]);
if(newDate > oldDate){
var date = new Date();
date.setDate(date.getDate() - 1);
var dd = date.getDate();
var mm = date.getMonth() + 1;
var yyyy = date.getFullYear();
if(dd < 10) {
dd = '0' +dd;
}
if(mm < 10) {
mm = '0' +mm;
}
var yesterday = dd+"."+mm+"."+yyyy;
getDataHistory('add' + yesterday);
reset();
}
newDate = new Date().toLocaleDateString('de-CH', {
weekday: 'long',
day: '2-digit',
month: '2-digit',
year: 'numeric'
});
document.getElementById('date').innerHTML = newDate;
}
getDataHistory('new');
function loadJson(request){
createTable(request.responseText);
}
function createHeader(array){
var header = document.createElement("thead");
var hRow = document.createElement('tr');
hRow.classList.add('header');
for(var i = 0; i < array.length; i++){
var div = document.createElement('div');
var th = document.createElement('th');
div.innerHTML = array[i];
th.appendChild(div);
hRow.appendChild(th);
}
header.appendChild(hRow);
return header;
}
function createTable(json){
var obj = JSON.parse(json);
var oldBody = document.getElementsByTagName('tbody')[0];
console.log(oldBody);
var oldHeader = document.getElementsByTagName('thead')[0];
var body = document.createElement('tbody');
var header = createHeader(["Name","09:00 – 09:45","10:15 – 11:00","11:00 – 11:45"," ","14:00 – 14:45","15:00 - 15:45","16:00 – 16:45"]);
for (var j = 0; j < obj.length; j++) {
var row = addRow(obj[j],body);
row.classList.add('row');
}
console.log(body);
replaceTable(body, oldBody, header ,oldHeader);
if(obj.length > 25){
var view = document.getElementById('home');
view.setAttribute("style", "overflow-y:scroll");
}
}
function toInput(cell){
var input = document.createElement('input');
setTimeout(function() { input.focus(); }, 200);
cell.appendChild(input);
window.addEventListener('keypress', function(e){
if(e.keyCode == '13'){
var text = input.value;
if(input.parentNode != null){
input.parentNode.removeChild(input);
}
cell.innerHTML = text;
getData("get"+getString(cell.parentNode.cells[0].childNodes[0].innerHTML));
}
}, false);
}
function replaceTable(body, oldBody, header, oldHeader){
if(typeof oldHeader == 'undefined'){
table.appendChild(header);
}else if(oldHeader.parentNode == table){
table.replaceChild(header, oldHeader);
}else{
table.appendChild(header);
}
if(typeof oldBody == 'undefined'){
table.appendChild(body);
}else if(oldBody.parentNode == table){
table.removeChild(oldBody);
table.appendChild(body);
//table.replaceChild(body, oldBody);
}else{
table.appendChild(body);
}
}
function addRow(val,body) {
var rest = val.split(";");
var tr = document.createElement('tr');
for( var i = 0; i < 8; i++){
if(i==0){
var name = rest[0];
addCell(tr, null,name);
}else{
var value = rest[i];
addCell(tr, value, name);
}
}
body.appendChild(tr);
return tr;
}
function addCell(tr, val, name) {
var name;
var cell = document.createElement('td');
var value = "get";
if(val == null){
var input = document.createElement('label');
cell.classList.add("name")
input.innerHTML = name;
input.readOnly = true;
cell.appendChild(input);
}else{
cell = document.createElement('td');
cell.classList.add('cell');
var content = val.split(":");
switch(content[0]){
case '0': cell.classList.add('ext'); break;
case '1': cell.classList.add('les'); break;
case '2': cell.classList.add('term'); break;
case '3': cell.classList.add('on'); break;
case '4': cell.classList.add('spacer'); break;
}
if(val.length > 1){
cell.innerHTML = content[1];
}
}
tr.appendChild(cell);
}
window.onclick = function(event) {
if (!event.target.matches('.dropA')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
function getString(name){
var x = "";
var names = document.getElementsByClassName('name');
var values = document.getElementsByClassName('cell');
for(var i = 0;i<names.length;i++){
if(names[i].childNodes[0].innerHTML == name){
x+= names[i].childNodes[0].innerHTML + ";";
for(var j = (7 * i); j < (7 * i) + 7 ; j++){
switch(""+values[j].classList){
case 'cell': x += "0"; break;
case 'cell ext': x += "0"; break;
case 'cell les': x += "1"; break;
case 'cell term': x += "2"; break;
case 'cell on': x += "3"; break;
case 'cell spacer': x += "4"; break;
}
if(values[j].innerHTML != "" && values[j].innerHTML != null){
x+= ":" + values[j].innerHTML
}
x += ";";
}
}
}
return x;
}
function getResetString(){
var names = document.getElementsByClassName('name');
var x = "";
for(var i = 0; i < names.length; i++){
x += names[i].value +";";
for (var j = 0; j < 7 ; j++){
if(j == 3){
x += "4";
}else{
x += "0";
}
x += ";";
}
if(i < names.length-1){
x+="|";
}
}
return x;
}
it seems a tiny problem. on your css, you have to add an space on the calc statement:
height:calc(100% - 50px);
I was able to make it work in here
https://codesandbox.io/s/vibrant-http-ty85k
You can add display: table; to #container
and add:
#legend {
display: table-row;
}
This should work, but I think you should refactor (simplify) the whole page and styles.

I want my Game to be at the bottom not at the top?

I have a school project where i want to programm a Gaming site but the game i copied from another website is always at the top can you help me to bring it down ?
I want the Game to be in the Center of the Website under the Hotbar :)
HTML CODE:
<!DOCTYPE html>
<html>
<head>
<title>Games</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="css/nav.css">
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
</style>
</head>
<body onload="startGame()">
<div class="nav">
<ul>
<li>Aventure Games</li>
<li>1vs1 Games</li>
<li id="logo"><img src="src/logogif.gif" style="width:300px;"></li>
<li>Other Categories</li>
<li>About us</li>
</ul>
</div>
<article>
<h1>Games</h1>
<p>content bla bla</p>
</article>
<p class="flappybird">
<script>
var myGamePiece;
var myObstacles = [];
var myScore;
function startGame() {
myGamePiece = new component(30, 30, "red", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function accelerate(n) {
myGamePiece.gravity = n;
}
</script>
<br>
<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
<p>Use the ACCELERATE button to stay in the air</p>
<p>How long can you stay alive?</p>
</body>
</html>
CSS CODE:
*{
margin: 0px;
background-color:darkred;
}
.nav{
height: 100px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
text-align: center;
/*padding-left: 30%;
padding-right: 30%;*/
/*overflow: hidden;*/
background-color: gray;
height:70px;
}
li {
/*float: left;*/
display: inline-block;
vertical-align: top;
margin: 0 -2px;
}
li a {
display: block;
font-family: Verdana, Helvetica, sans-serif;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
background-color: darkred;
}
#logo {
/*display: block;*/
font-family: Verdana, Helvetica, sans-serif;
background-color: black;
text-align: center;
padding: 10px;
text-decoration: none;
}
#logo img{
display: block;
}
li a:hover {
background-color: black;
color: white;
}
}
p{font-family:Tahoma;}
article {
padding-left: 12px;
}
You've got to define the correct place you want to insert your new element, in your case the canvas.
In your code snippet below, you tell the canvas to be placed before the first child node of the document body.
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
You need to specify an element to place it before, below it's looking for the first element with the class name flappybird.
document.body.insertBefore(this.canvas, document.getElementsByClassName("flappybird")[0]);

How can I check the pixel location of my cursor?

On any given application, is there a way, maybe in dev tools where I can check the pixel location of my mouse hover?
Use javascript to get cursor location.
document.addEventListener("mouseover", function( event ) {
console.log(event.screenX, event.screenY);
}, false);
Getting the Mouse Click Position :
<!DOCTYPE html>
<html>
<head>
<title>Move to Click Position</title>
<style type="text/css">
body {
background-color: #FFF;
margin: 30px;
margin-top: 10px;
}
#contentContainer {
width: 550px;
height: 350px;
border: 5px black solid;
overflow: hidden;
background-color: #F2F2F2;
cursor: pointer;
}
#thing {
position: relative;
left: 50px;
top: 50px;
transition: left .5s ease-in, top .5s ease-in;
}
</style>
</head>
<body>
<div id="contentContainer">
<img id="thing" src="//www.kirupa.com/images/smiley_red.png">
</div>
<script src="//www.kirupa.com/prefixfree.min.js"></script>
<script>
var theThing = document.querySelector("#thing");
var container = document.querySelector("#contentContainer");
container.addEventListener("click", getClickPosition, false);
function getClickPosition(e) {
var parentPosition = getPosition(e.currentTarget);
var xPosition = e.clientX - parentPosition.x - (theThing.clientWidth / 2);
var yPosition = e.clientY - parentPosition.y - (theThing.clientHeight / 2);
theThing.style.left = xPosition + "px";
theThing.style.top = yPosition + "px";
}
// Helper function to get an element's exact position
function getPosition(el) {
var xPos = 0;
var yPos = 0;
while (el) {
if (el.tagName == "BODY") {
// deal with browser quirks with body/window/document and page scroll
var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
var yScroll = el.scrollTop || document.documentElement.scrollTop;
xPos += (el.offsetLeft - xScroll + el.clientLeft);
yPos += (el.offsetTop - yScroll + el.clientTop);
} else {
// for all other non-BODY elements
xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
yPos += (el.offsetTop - el.scrollTop + el.clientTop);
}
el = el.offsetParent;
}
return {
x: xPos,
y: yPos
};
}
</script>
</body>
</html>
Refer : MouseEvent clientX Property & Getting the Mouse Click Position

Using CSS3 can you create this image masking effect?

I've seen some cool image masking and other effects with CSS3. Using only CSS3 can we achieve this effect seen in the code below or see working fiddle.
http://jsfiddle.net/s6u9a/
HTML
<canvas id="canvas1" width="400" height="400"></canvas>
Javascript
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
can.addEventListener('mousemove', function(e) {
var mouse = getMouse(e, can);
redraw(mouse);
}, false);
function redraw(mouse) {
console.log('a');
can.width = can.width;
ctx.drawImage(img, 0, 0);
ctx.beginPath();
ctx.rect(0,0,500,500);
ctx.arc(mouse.x, mouse.y, 70, 0, Math.PI*2, true)
ctx.clip();
ctx.drawImage(img2, 0, 0);
}
var img = new Image();
img.onload = function() {
redraw({x: -450, y: -500})
}
img.src = 'http://placekitten.com/400/400';
var img2 = new Image();
img2.onload = function() {
redraw({x: -450, y: -500})
}
img2.src = 'http://placekitten.com/400/395';
function getMouse(e, canvas) {
var element = canvas,
offsetX = 0,
offsetY = 0,
mx, my;
if (element.offsetParent !== undefined) {
do {
offsetX += element.offsetLeft;
offsetY += element.offsetTop;
} while ((element = element.offsetParent));
}
mx = e.pageX - offsetX;
my = e.pageY - offsetY;
return {
x: mx,
y: my
};
}
Here's an alternate approach using background-image. It's more flexible, and you get a circular (or arbitrarily shaped) viewport:
http://jsfiddle.net/maackle/66uCs/
HTML
<div class="masker">
<img class="base" src="http://lorempixel.com/400/400/cats/1" />
<div class="overlay"></div>
</div>
CSS
.masker {
position: relative;
}
.overlay {
position: absolute;
display: none;
width: 100px;
height: 100px;
border-radius: 50%;
background: url(http://lorempixel.com/400/400/cats/2) no-repeat;
}
jQuery 1.10.1
$('.masker').on('mousemove', function (e) {
var r, x, y, top, left, bottom, right, attr, $base, $overlay;
r = 100;
$base = $(this).find('.base');
$overlay = $(this).find('.overlay');
x = e.pageX - $base.offset().left;
y = e.pageY - $base.offset().top;
top = y - r / 2;
left = x - r / 2;
if (x < 0 || y < 0 || x > $base.width() || y > $base.height()) {
$overlay.hide();
} else {
$overlay.show().css({
'background-position': '' + (-left) + 'px ' + (-top) + 'px',
'left': left,
'top': top
});
}
});
//just for good measure
$('.masker').on('mouseout', function (e) {
$(this).find('.overlay').hide();
});
Here's a solution that uses mostly CSS -- you do need some Javascript to detect the mouse position. Note that this gives you a square viewport instead of a circle, but if CSS ever adds circle() alongside rect() for the clip property, you will have that option:
http://jsfiddle.net/maackle/Yc2b4/
HTML
<div class="masker">
<img class="base" src="http://lorempixel.com/400/400/cats/1" />
<img class="overlay" src="http://lorempixel.com/400/400/cats/2" />
</div>
CSS
.masker {
position: relative;
}
.overlay {
position: absolute;
top: 0;
left: 0;
display: none;
}
jQuery 1.10.1
$('.masker img').on('mousemove', function(e) {
var r, x, y, top, left, bottom, right, attr, $overlay;
r = 100;
x = e.pageX - $(this).offset().left;
y = e.pageY - $(this).offset().top;
top = y - r/2;
left = x - r/2;
bottom = y + r/2;
right = x + r/2;
attr = 'rect('+(top)+'px, '+(right)+'px, '+(bottom)+'px, '+(left)+'px)';
$overlay = $('.masker .overlay');
$overlay.show().css({clip: attr});
});
$('.masker img').on('mouseout', function(e) {
$('.masker .overlay').hide();
});