Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double display of canvas element

The drawn lines appear not only in the Canvas-Element but also at the top of the page it is displayed again.

This happens when a touch event is been released.

Here you can find the source code and the result (PhoneGap and Jquery Mobile is used).

Do anybody has an idea what’s the reason for this fault?

http://gomami.ch/bugs/screenshot.png http://gomami.ch/bugs/screenshot.png

JavaScript (loaded in header after includes to jQuery/PhoneGap):

//*********************************************************
// Wait for Cordova to Load
//*********************************************************
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
  canvas("aufprallstelleA","test");
}

// function to setup a new canvas for drawing
function canvas(canvas, element){
    //define and resize canvas
    width = $(window).width();
    width = (width/100)*90;
    height = width/1.6901408;
    $("#content").width(width);
    $("#"+element).height(height);
    $("#"+element).width(width);
    var html = 'This is de Canvas field wehre you can draw<br><canvas id="'+
               canvas+'" width="'+width+'" height="'+height+
               '" style="border:black solid 1px;background-size: 100%; background-repeat: no-repeat;background-image: url(\'images/allgemein/aufprallstelle.PNG\');"></canvas>';
    $("#"+element).html(html);      
    // setup canvas
    ctx=document.getElementById(canvas).getContext("2d");
    ctx.strokeStyle = "red";
    ctx.lineWidth = 5;          
    // setup to trigger drawing on mouse or touch
    $("#"+canvas).on("touchstart", start);
    $("#"+canvas).on("touchmove", move);
}

function start(e){
    e = e.originalEvent;
    x = e.changedTouches[0].pageX-20;
    y = e.changedTouches[0].pageY-$(this).position().top;
    ctx.moveTo(x,y);
}

function move(e){
    e.preventDefault();
    e = e.originalEvent;
    x = e.changedTouches[0].pageX-20;
    y = e.changedTouches[0].pageY-$(this).position().top;
    ctx.lineTo(x,y);
    ctx.stroke();
}

function clearcanvas(id) {
    var canvas = document.getElementById(id);
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height);
}    

HTML:

<div data-role="page" id="main">
  <div data-role="header" id="header">
    <div id="header_anzeige">
    </div>
  </div>
  <div data-role="content" id="content" >
    <div id="test" style="margin-top:265px;"></div><br>
    Hello, this is JQuery Mobile running in PhoneGap 2
  </div>   
</div> 
copyright
</div>
</div>
like image 670
Go Mami Avatar asked Apr 21 '15 14:04

Go Mami


People also ask

Can we have multiple canvas elements on one HTML?

Canvas Example The width and height attribute is necessary to define the size of the canvas. Tip: You can have multiple <canvas> elements on one HTML page. By default, the <canvas> element has no border and no content.

What is getContext 2d in canvas?

getContext("webgl", { antialias: false, depth: false, }); 2d context attributes: alpha. A boolean value that indicates if the canvas contains an alpha channel. If set to false , the browser now knows that the backdrop is always opaque, which can speed up drawing of transparent content and images.

What is a canvas element in HTML?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

Can HTML canvas have layers?

You can create multiple canvas elements without appending them into document. These will be your layers: Then do whatever you want with them and at the end just render their content in proper order at destination canvas using drawImage on context .


1 Answers

I've solved the issue by positionning the canvas in absolute instead of relative positionning as it's mentioned on https://github.com/jquery/jquery-mobile/issues/5107

like image 145
nonozor Avatar answered Oct 23 '22 17:10

nonozor