2013-05-26 11 views
7

Ich habe eine einfache Schublade Beispiel online gefunden. Es funktioniert gut auf dem PC.Kann nicht auf einem HTML5 Canvas mit Phonegap 2.7

Wenn ich es mit Phonegap 2.7, auf meinem Galaxy S4 (4.2.2), mit Android 2.2 oder 4.2.2, für das Projekt läuft, zeichnet es überhaupt nichts.

Was mache ich falsch?

<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>Desktops and Tablets</title> 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
    initialize(); 
    }); 

    // works out the X, Y position of the click inside the canvas from the X, Y position on the page 
    function getPosition(mouseEvent, sigCanvas) { 
    var x, y; 
    if (mouseEvent.pageX != undefined && mouseEvent.pageY != undefined) { 
     x = mouseEvent.pageX; 
     y = mouseEvent.pageY; 
    } else { 
     x = mouseEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
     y = mouseEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
    } 

    return { X: x - sigCanvas.offsetLeft, Y: y - sigCanvas.offsetTop }; 
    } 

    var sigCanvas; 
    var context; 
    function initialize() { 
    sigCanvas = document.getElementById("canvasSignature"); 
    context = sigCanvas.getContext("2d"); 
    context.strokeStyle = 'Black'; 
    context.lineWidth = 1; 
    if ('ontouchstart' in document.documentElement) { 
     var drawer = { 
     isDrawing: false, 
     touchstart: function (coors) { 
       context.beginPath(); 
       context.moveTo(coors.x, coors.y); 
       this.isDrawing = true; 
      }, 
      touchmove: function (coors) { 
       if (this.isDrawing) { 
       context.lineTo(coors.x, coors.y); 
       context.stroke(); 
       } 
      }, 
      touchend: function (coors) { 
       if (this.isDrawing) { 
       this.touchmove(coors); 
       this.isDrawing = false; 
       } 
      } 
     }; 

     // create a function to pass touch events and coordinates to drawer 
     function draw(event) { 
      if (event.targetTouches[0] === undefined) { 
       context.closePath(); 
       this.isDrawing = false; 
       return; 
      } 
      // get the touch coordinates. Using the first touch in case of multi-touch 
      var coors = { 
       x: event.targetTouches[0].pageX, 
       y: event.targetTouches[0].pageY 
      }; 
      // Now we need to get the offset of the canvas location 
      var obj = sigCanvas; 
      if (obj.offsetParent) { 
       // Every time we find a new object, we add its offsetLeft and offsetTop to curleft and curtop. 
       do { 
       coors.x -= obj.offsetLeft; 
       coors.y -= obj.offsetTop; 
       } 
       // The while loop can be "while (obj = obj.offsetParent)" only, which does return null 
       // when null is passed back, but that creates a warning in some editors (i.e. VS2010). 
       while ((obj = obj.offsetParent) != null); 
      } 

      // pass the coordinates to the appropriate handler 
      drawer[event.type](coors); 
     } 
     // attach the touchstart, touchmove, touchend event listeners. 
     sigCanvas.addEventListener('touchstart', draw, false); 
     sigCanvas.addEventListener('touchmove', draw, false); 
     sigCanvas.addEventListener('touchend', draw, false); 
     // prevent elastic scrolling 
     sigCanvas.addEventListener('touchmove', function (event) { 
      event.preventDefault(); 
     }, false); 
    } 
    else { 
     // start drawing when the mousedown event fires, and attach handlers to 
     // draw a line to wherever the mouse moves to 
     $("#canvasSignature").mousedown(function (mouseEvent) { 
      var position = getPosition(mouseEvent, sigCanvas); 

      context.moveTo(position.X, position.Y); 
      context.beginPath(); 
      $(this).mousemove(function (mouseEvent) { 
       drawLine(mouseEvent, sigCanvas, context); 
      }).mouseup(function (mouseEvent) { 
       finishDrawing(mouseEvent, sigCanvas, context); 
      }).mouseout(function (mouseEvent) { 
       finishDrawing(mouseEvent, sigCanvas, context); 
      }); 
     }); 

    } 
    } 

    // draws a line to the x and y coordinates of the mouse event inside 
    // the specified element using the specified context 
    function drawLine(mouseEvent, sigCanvas, context) { 
     var position = getPosition(mouseEvent, sigCanvas); 

    context.lineTo(position.X, position.Y); 
    context.stroke(); 
    } 

    // draws a line from the last coordiantes in the path to the finishing 
    // coordinates and unbind any event handlers which need to be preceded 
    // by the mouse down event 
    function finishDrawing(mouseEvent, sigCanvas, context) { 
    // draw the line to the finishing coordinates 
    drawLine(mouseEvent, sigCanvas, context); 
     context.closePath(); 
     // unbind any events which could draw 
    $(sigCanvas).unbind("mousemove") 
       .unbind("mouseup") 
       .unbind("mouseout"); 
    } 
</script> 
</head> 
<body> 
<h1>Canvas test</h1> 
<div id="canvasDiv"> 
    <canvas id="canvasSignature" width="500px" height="500px" style="border:2px solid #000000;"></canvas> 
</div> 
</body> 
</html> 

Antwort

5

Ich habe das gleiche Problem auf Galaxy S4. HTML-Canvas-Zeichnung wird nicht in der Webansicht angezeigt, aber sie funktioniert im Safari-Browser. Galaxy S2, S3 haben nicht das gleiche Problem.

In meinem Fall, wenn ich Hardwarebeschleunigung deaktivieren, Canvas funktioniert.

appView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 

aber es ist sehr langsam. Es hat nichts mit PhoneGap zu tun. Wahrscheinlich hat Samsung einen weiteren Fehler bei der Implementierung gemacht.

+0

DANKE SO VIEL !!! :) –

+0

Die Deaktivierung der Hardwarebeschleunigung lässt die Dinge "funktionieren", ist aber keine sehr gute Produktionslösung für eine Arbeitsfläche. Leider habe ich keine besseren Vorschläge. – Josh

0

Bauen Sie mit Phonegap Build oder Eclipse? Haben Sie Ihr Projekt bei Eclipse korrekt eingerichtet?

Ersetzen Sie $(document).ready(function() { durch $(document).on("deviceready",function() { , da phonegap das deviceready-Ereignis verwendet, um zu signalisieren, wenn es initialisiert wurde.

Speichern Sie auch die jQuery JS-Datei in Ihrem Projekt; Wenn die HTTP-Anfrage fehlschlägt oder Ihr Android-Gerät keine Verbindung hat, kann die App nicht geladen werden, da jQuery nicht geladen werden kann.

ersetzen Deshalb <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

mit <script type="text/javascript" src="jquery.min.js"></script>

ich den obigen Code mit diesen Modifikationen ausprobiert (plus Ich habe einige Debug) und es funktioniert auf meinem HTC HD2 feine Android 2.2.3 laufen und mein Nexus 7 läuft Android 4.2.2

Here's a zip file enthält das Eclipse-Projekt, das ich erstellt habe, um es zu testen und resultierende Android APK. Probieren Sie es auf Ihren Geräten aus und sehen Sie, ob es funktioniert.

+0

Danke für Ihren Kommentar, .. (Der Link, den Sie wird ab sofort gebucht gebrochen) Ich tat alles, was Sie geschrieben haben, aber es hat das Problem nicht beheben, öffnet sich die Leinwand, aber wenn Ich berühre es (mit meinem Galaxy S4 4.2.2) - nichts passiert .. –

+0

Link funktioniert immer noch für mich, versuche es noch einmal: [http://ge.tt/9kqdrmi/v/0](http://ge. tt/9kqdrmi/v/0) – DaveAlden

+0

Ja, jetzt funktioniert es, aber immer noch, Ihr Projekt 'as is' funktioniert auch nicht .. Ich denke, das ist ein Galaxy S4 Problem? –

1

Meine Galaxy S4 "Bugs" aus, wenn ich versuche, ein Bild auf die Koordinaten 0,0 zu zeichnen.

Ich habe das nur erkannt, weil ich Elemente innerhalb der Zeichenfläche verschiebe und welche Farbe auch immer zu diesen Koordinaten gezeichnet wird, deckt den ganzen Bildschirm ab.

Wenn ich an einen Punkt ziehe, an dem kein Bild die 0,0 Koordinaten im Canvas abdeckt, läuft meine Anwendung normal. Ist auch nie im Desktop-Browser passiert. Sehr eigenartig.

Ich laufe gerade:

context.clearRect(0,0,1,1); 

am Ende meiner Ziehfunktion, und es löste es für mich. Einen Versuch wert mindestens.

Das Galaxy S3 meines Freundes hat dies jedoch nicht getan.

0

Sie sollten Crosswalk verwenden.Es ersetzt die standardmäßige Android-Webansicht, die sehr inkonsistent ist (jeder Anbieter führt normalerweise seine eigenen), und es ist schwierig, Ihre Anwendung unter allen Android-Versionen und -Anbietern konsistent zu halten.

Crosswalk hat eine Cordova-kompatible Version und in allen meinen Tests ist die Leistungssteigerung spürbar.

https://crosswalk-project.org/