Reputation: 131
I have a weird bug that only lets my Javascript code run properly if the Chrome developer tools is open. I have read all the other articles with different solutions including removing all console.log() statements and disabling caching for jQuery AJAX (I am not using jQuery) but they do not work.
When it boils down, I have a navigation bar, a video container, 9 pictures, then I have a canvas that I wish to draw on. The code is supposed to work on the canvas to allow drawing, but only works with chrome developer tools open.
The peculiar thing is if I remove 6 pictures such that I have only have 2 pictures remaining above the canvas, the canvas can be drawn upon without opening developer tools.
I don't think it is an issue of "being drawn but just not appearing on the screen" since I have the correct offsets for the drawings to appear, and even if I didn't, it should not work even if I have developer tools open.
Here is the simplified html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/templatemo-style.css">
<link href="https://fonts.googleapis.com/css?family=Kanit:100,200,300,400,500,600,700,800,900" rel="stylesheet">
</head>
<body>
<nav>
<div class="logo">
<a href="index.html">Buy <em>High, </em>Sell <em>Low</em> </a>
</div>
<div class="logo1">
<a href="https://jayvyer.github.io/">Javier <em>Liu</em></a>
</div>
</nav>
<div id="video-container">
<div class="video-overlay"></div>
<div class="video-content">
<div class="inner">
<h1>Welcome to <em>BHSL</em></h1>
<p style="font-size: 36px">Basic Trading Patterns</p>
<p style="font-size: 10px">Always buy at highest, sell at cheapest!</p>
<p style="font-size: 6px">Don't actually do this. Not financial advice.</p>
<div class="scroll-icon">
<a class="scrollTo" href="#portfolio"><img src="img/scroll-icon.png" alt=""></a>
</div>
</div>
</div>
<video autoplay="" loop="" muted>
<source src="increase_stonk.mp4" type="video/mp4" />
</video>
</div>
<div class="full-screen-portfolio" id="portfolio">
<div class="container-fluid">
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="triangles.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Ascending & Descending<em>Triangles</em></h1>
<p>Continuation Pattern</p>
</div>
</div>
<div class="image">
<img src="img/ascending_triangle5.png">
</div>
</a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="cup-handle.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Cup <em> and Handle</em></h1>
<p>Continuation Pattern</p>
</div>
</div>
<div class="image">
<img src="img/cuphandle.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="flag-pennant.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Flags <em> and Pennants</em></h1>
<p>Continuation Pattern</p>
</div>
</div>
<div class="image">
<img src="img/flagpennant.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="rectangles.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Top and Bottom <em> Rectangles</em></h1>
<p>Continuation Pattern</p>
</div>
</div>
<div class="image">
<img src="img/rectangle.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="doubles.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Double <em> Tops & Bottoms</em></h1>
<p>Reversal Pattern</p>
</div>
</div>
<div class="image">
<img src="img/double.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="triples.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Triple <em> Tops & Bottoms</em></h1>
<p>Reversal Pattern</p>
</div>
</div>
<div class="image">
<img src="img/triple.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="wedges.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Falling & Rising <em> Wedges</em></h1>
<p>Reversal Pattern</p>
</div>
</div>
<div class="image">
<img src="img/wedge.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="2in1dandruffshampoo.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Head and <em>Shoulders</em></h1>
<p>Reversal Pattern</p>
</div>
</div>
<div class="image">
<img src="img/headshoulders.png">
</div></a>
</div>
</div>
<div class="col-md-4 col-sm-6">
<div class="portfolio-item">
<a href="diamonds.html">
<div class="hover-effect">
<div class="hover-content">
<h1>Diamond <em>Tops & Bottoms</em></h1>
<p>Reversal Pattern</p>
</div>
</div>
<div class="image">
<img src="img/diamond.png">
</div></a>
</div>
</div>
</div>
<div class="canvas_" style="text-align:center;background-color: #011423;">
<canvas id="can" width="400" height="400" style="top:0%;left: 10%;right: 500%;border:2px solid;margin: 20px;border-radius: 35px; border-width: medium; border-color: #311746;"></canvas>
<script type="text/javascript" src="C:\Users\javie\Documents\Recognizer\bundle.js"></script>
<!-- <div class="">
<span style="margin-right: 50px;"><button onclick="erase()" id="clearboard" style="background-color: #000000; border-radius: 50px; color: #ffffff">Clear Board</button></span>
<span><button onclick="fnClick()" id="my-button2" style="background-color: #000000; border-radius: 50px; color: #ffffff">Calculate Similarity</button></span>
<br><br>
</div> -->
</div>
</div>
</body>
</html>
This is the JavaScript file:
var canvas, ctx, flag = false, prevX = 0, currX = 0, prevY = 0, currY = 0, dot_flag = false, coordinates = [];
var curvematcher = require('curve-matcher')
const init = () => {
canvas = document.getElementById('can');
canvas.style.backgroundColor = "#fffcf5";
ctx = canvas.getContext("2d");
ctx.canvas.width = window.innerWidth * .80;
w = canvas.width;
h = canvas.height;
canvas.addEventListener("mousemove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e)
}, false);
}
const draw = () => {
ctx.beginPath();
ctx.moveTo(prevX, prevY + 669);
ctx.lineTo(currX, currY + 669);
console.log("x: " + currX + " y: " + currY);
coordinates.push({x:prevX, y: prevY + 669});
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.stroke();
ctx.closePath();
}
const erase = () => {
var m = confirm("Are you sure you want to clear the entire board?");
// coordinates.forEach((item, i) => {
// console.log(" {x: " + coordinates[i].x + ", y: " + coordinates[i].y + "},");
// });
if (m) {
coordinates = [];
ctx.clearRect(0, 0, w, h);
}
}
const findxy = (res, e) => {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
flag = true;
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
draw();
}
}
}
init()
Like I said, what really bugs me is that if I move the canvas up to the top, or if I remove some images, then the canvas can be drawn upon properly. I feel like if I put the canvas on another page then it would defintely work, but I don't want to be forced to have the canvas separate from the images, as they are important together, and I would immensely prefer if it were below the images.
Is there an issue regarding how many images can be on a page? Or perhaps a constraint on the flow of canvases on a page?
Upvotes: 1
Views: 980
Reputation: 9699
You have incorrect calculations for xy, which goes beyond canvas.
If your canvas is located down in the page with the scroll
canvas.offsetTop
would be amount the space from the start of the scroll (grows to infinity when your scroll grows), but e.clientY
would grow to window height (display height) which is a fix number.
I would suggest using mouseEvent.offsetX which is supported in ie9+ and test it in different conditions on different browsers. I tested it in Chrome and it works perfect w/ and w/o scroll.
const findxy = (res, e) => {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.offsetX;
currY = e.offsetY;
flag = true;
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.offsetX;
currY = e.offsetY;
draw();
}
}
}
Upvotes: 1