I'm am VERY inexperienced in webdev, but here i am making a website. Im using Fabric.js and a script from an answer from stackoverflow so that the user can drag an image from one div into a canvas. When i test it out i always get the error "Cannot call method 'addEventListener' of null". I guess it means that what ever the script is trying to load, it hasn't loaded thus leaving it as null. The console says its from line 77 (canvasContainer.addEventListener('dragenter'..., near the bottom) in my JS file. I've tried moving the import script code lower down (under body) so that it loads after everything else but it still doesn't work. Any ideas?
PS. the the javascript comes from natchiketa.
HTML:
<!doctype html>
<html lang="en";>
<head>
<meta charset="utf-8" />
<title>CustomCase</title>
<link rel="stylesheet" href="SkapaDesign.css">
<script src="Jquery.js"></script>
<script src="Modernizr.js"></script>
<script src="Fabric.js"></script>
<script src="Canvas.js"></script>
</head>
<body>
<div id="Wrapper">
<section id="Body">
<div id="CanvasDiv">
<canvas id="Canvas" width="270px" height="519px"></canvas>
</div>
<div id="TextureView">
<div id="TextureViewInside">
<ul class="products">
<img draggable="true" src="FärgadePapper.png">
<img draggable="true" src="Hajar.png">
<img draggable="true" src="Labyrint.png">
<img draggable="true" src="Martini.png">
</ul>
</div>
</div>
</section>
</div>
</body>
</html>
CSS:
#Body{
height: 675px;
}
#Canvas, #TextureView{
float: left;
}
#LeftColumn{
float: left;
width: 355px;
margin: 20px 30px 0px 0px;
}
#RightColumn{
float: left;
width: 600px;
margin-top: 20px;
}
#CanvasDiv{
width: 270px;
height: 519px;
margin-left: 15px;
}
#Canvas{
overflow: hidden;
}
#TextureView{
width: 470px;
height: 559px;
overflow: auto;
}
#TextureViewInside{
padding: 10px;
}
ul.products img{
width: 97px;
display: inline-block;
vertical-align: top;
padding: 4px;
}
JavaScript:
var canvas = new fabric.Canvas('Canvas');
function handleDragStart(e) {
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
this.classList.add('img_dragging');
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'copy';
return false;
}
function handleDragEnter(e) {
this.classList.add('over');
}
function handleDragLeave(e) {
this.classList.remove('over');
}
function handleDrop(e) {
if (e.stopPropagation) {
e.stopPropagation();
}
var img = document.querySelector('#images img.img_dragging');
console.log('event: ', e);
var newImage = new fabric.Image(img, {
width: img.width,
height: img.height,
left: e.layerX,
top: e.layerY
});
canvas.add(newImage);
return false;
}
function handleDragEnd(e) {
[].forEach.call(images, function (img) {
img.classList.remove('img_dragging');
});
}
if (Modernizr.draganddrop) {
var images = document.querySelectorAll('#images img');
[].forEach.call(images, function (img) {
img.addEventListener('dragstart', handleDragStart, false);
img.addEventListener('dragend', handleDragEnd, false);
});
var canvasContainer = document.getElementById('CanvasDiv');
canvasContainer.addEventListener('dragenter', handleDragEnter, false);
canvasContainer.addEventListener('dragover', handleDragOver, false);
canvasContainer.addEventListener('dragleave', handleDragLeave, false);
canvasContainer.addEventListener('drop', handleDrop, false);
} else {
alert("This browser doesn't support the HTML5 Drag and Drop API.");
}