0
I am working on a web application that has a framework, this framework was developed in ASP.net, where I can add javascript to the screens. On one of the screens I added a digital signature, in which you write to a "canvas" frame as you detect mouse or touch events, then store the data in the database.
This javascript executes an onload inside an onready by appendchild an element of the source screen. This way I add my digital signature to the original screen.
My digital signature is a responsive Canvas 2d. With the responsive canvas, you need to have a backup of the canvas whenever it receives changes in dimensions, because whenever the canvas dimensions are changed all its contents are cleaned. In this way, I added a window.onresize event that draws my backup on canvas, after setting width and height.
In the next section of this question, I will leave the bold analysis from the programmer’s point of view, in standard Bold user-side analysis.
The user loads the page, are fired onready, onload, within the onload add elements to the DOM
var stringAssina = "<canvas id="quadro" style="display:block;overflow: hidden;width: calc(100% - 10px);height:400px;border:1px solid"></canvas>";
jQuery(document).ready(function() {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(function () {
jQuery(stringAssina).insertAfter("#ctl00_conteudo_pbDadosMB");
// recarrega canvas com dimensões da janela
window.onresize = function(event) {
var quadro = document.getElementById('quadro');
quadro.width = quadro.clientWidth;
quadro.height = quadro.clientHeight;
console.log('resize');
var img = new Image;
img.onload = function() {
var ctx = quadro.getContext("2d");
ctx.drawImage(img, 0, 0, quadro.clientWidth, quadro.clientHeight);
};
img.src = dataURL;
At this time the user with the mouse or touch events, when trying to write to the canvas. You will be writing in places that do not match the mouse coordinates. This is because canvas.width and canvas.height are undifined. To solve this problem still in onload, I need to set in canvas.width and canvas.height, this way we will be writing in the right places. So still on onload I have
var img = new Image;
var quadro = document.getElementById('quadro');
console.log(quadro.clientWidth);
quadro.width = quadro.offsetWidth;
quadro.height = quadro.offsetHeight;
var img = new Image;
img.onload = function() {
var ctx = quadro.getContext("2d");
ctx.drawImage(img, 0, 0, quadro.offsetWidth, quadro.offsetHeight);
};
img.src = dataURL;
This code sets the canvas dimensions to 0. That is to say the frame.offsetWidth and quandro.offsetHeight are 0.
Completion: My code works from the moment I reset the window. The offsetWidth and offsetHeight canvas are 0.
Note: The above code did not add the writing events to the canvas, these events exist within onload.
[UPDATE - 11-06-2019 14:18]
I started doing the insertAfter inside the ready. The clientWidth and the canvas offsetWidth, continue to be 0 in the first occurrence of onload. I was able to solve by going to fetch the width of the parent, however ethically it is not efficient, because it should be possible to search the width of the canvas in the first onload.
Thanks for your input. It actually improved my code, however the canvas clientWidth remains 0 on onload. I managed to solve alternatively, I took the parent element and went to get the offsetWidth.
– Pedro C.
When I inserted the element dynamically in onload (now with your help I put in onready) I could take it in the width of the parent, and set the canvas. However, something set it back to 0.
– Pedro C.
Perhaps some content that was added on the page made the elements adjust the dimensions, but it is a long shot this theory. I’m glad you decided!
– iamdlm
New problems have arisen on this page where I am adding elements via javascript. There are buttons that make you run onload, without running ready. I say this because the elements I insert in ready, no longer appear in html, or DOM.
– Pedro C.
For
onload
you mean thewindow.onload
? It is not correct to have buttons running this method. You can create a Jsfiddle with the part of code you need functional help?– iamdlm
I’m working on a web application, it has a framework area for programmers that allows you to add javascript to your computer. These buttons that you call window.onloads are the source of the web application. This is the disadvantage of working on frameworks, not knowing how it is programmed behind the programming that is being done by me.
– Pedro C.
I managed to go around, no onload fiz if(!Document.getElementById(mycanvas)) I add the canvas and set the dimensions of the father.
– Pedro C.
If you consider that the initial answer was valid to solve the problem mark as correct please
– iamdlm