-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
To compile in the cloud, check out build.phonegap.com
var PhoneGapReady = false;
var jQueryReady = false;
var panning = false;
var zooming = false;
var startX0;
var startY0;
var startX1;
var startY1;
var endX0;
var endY0;
var endX1;
var endY1;
var startDistanceBetweenFingers;
var endDistanceBetweenFingers;
var pinchRatio;
var imgWidth = 200;
var imgHeight = 300;
var currentContinuousZoom = 1.0;
var currentOffsetX = -100;
var currentOffsetY = -100;
var currentWidth = imgWidth;
var currentHeight = imgHeight;
var newContinuousZoom;
var newHeight;
var newWidth;
var newOffsetX;
var newOffsetY;
var centerPointStartX;
var centerPointStartY;
var centerPointEndX;
var centerPointEndY;
var translateFromZoomingX;
var translateFromZoomingY;
var translateFromTranslatingX;
var translateFromTranslatingY;
var translateTotalX;
var translateTotalY;
var percentageOfImageAtPinchPointX;
var percentageOfImageAtPinchPointY;
var theImage;
function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
/* When this function is called, PhoneGap has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
function onDeviceReady()
{
PhoneGapReady = true;
if (jQueryReady == true)
{
PhoneGapAndjQueryReady();
}
}
$(document).bind("mobileinit", function()
{
// jQuery Mobile framework configuration changes
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
jQueryReady = true;
if (PhoneGapReady == true)
{
PhoneGapAndjQueryReady();
}
});
function PhoneGapAndjQueryReady()
{
theImage = document.getElementById("eiffelTower");
theImage.height = imgHeight;
theImage.width = imgWidth;
theImage.style.left = currentOffsetX + "px";
theImage.style.top = currentOffsetY + "px";
theImage.ontouchstart = touchStart;
theImage.ontouchmove = touchMove;
theImage.ontouchend = touchEnd;
theImage.ontouchcancel = touchCancel;
}
function touchStart(event)
{
panning = false;
zooming = false;
if (event.touches.length == 1) {
panning = true;
startX0 = event.touches[0].pageX;
startY0 = event.touches[0].pageY;
}
if (event.touches.length == 2) {
zooming = true;
startX0 = event.touches[0].pageX;
startY0 = event.touches[0].pageY;
startX1 = event.touches[1].pageX;
startY1 = event.touches[1].pageY;
centerPointStartX = ((startX0 + startX1) / 2.0);
centerPointStartY = ((startY0 + startY1) / 2.0);
percentageOfImageAtPinchPointX = (centerPointStartX - currentOffsetX)/currentWidth;
percentageOfImageAtPinchPointY = (centerPointStartY - currentOffsetY)/currentHeight;
startDistanceBetweenFingers = Math.sqrt( Math.pow((startX1-startX0),2) + Math.pow((startY1-startY0),2) );
}
}
function touchMove(event)
{
// This keeps touch events from moving the entire window.
event.preventDefault();
if (panning) {
endX0 = event.touches[0].pageX;
endY0 = event.touches[0].pageY;
translateFromTranslatingX = endX0 - startX0;
translateFromTranslatingY = endY0 - startY0;
newOffsetX = currentOffsetX + translateFromTranslatingX;
newOffsetY = currentOffsetY + translateFromTranslatingY;
theImage.style.left = newOffsetX + "px";
theImage.style.top = newOffsetY + "px";
}
else if (zooming) {
// Get the new touches
endX0 = event.touches[0].pageX;
endY0 = event.touches[0].pageY;
endX1 = event.touches[1].pageX;
endY1 = event.touches[1].pageY;
// Calculate current distance between points to get new-to-old pinch ratio and calc width and height
endDistanceBetweenFingers = Math.sqrt( Math.pow((endX1-endX0),2) + Math.pow((endY1-endY0),2) );
pinchRatio = endDistanceBetweenFingers/startDistanceBetweenFingers;
newContinuousZoom = pinchRatio * currentContinuousZoom;
newWidth = imgWidth * newContinuousZoom;
newHeight = imgHeight * newContinuousZoom;
// Get the point between the two touches, relative to upper-left corner of image
centerPointEndX = ((endX0 + endX1) / 2.0);
centerPointEndY = ((endY0 + endY1) / 2.0);
// This is the translation due to pinch-zooming
translateFromZoomingX = (currentWidth - newWidth) * percentageOfImageAtPinchPointX;
translateFromZoomingY = (currentHeight - newHeight) * percentageOfImageAtPinchPointY;
// And this is the translation due to translation of the centerpoint between the two fingers
translateFromTranslatingX = centerPointEndX - centerPointStartX;
translateFromTranslatingY = centerPointEndY - centerPointStartY;
// Total translation is from two components: (1) changing height and width from zooming and (2) from the two fingers translating in unity
translateTotalX = translateFromZoomingX + translateFromTranslatingX;
translateTotalY = translateFromZoomingY + translateFromTranslatingY;
// the new offset is the old/current one plus the total translation component
newOffsetX = currentOffsetX + translateTotalX;
newOffsetY = currentOffsetY + translateTotalY;
// Set the image attributes on the page
theImage.style.left = newOffsetX + "px";
theImage.style.top = newOffsetY + "px";
theImage.width = newWidth;
theImage.height = newHeight;
}
}
function touchEnd(event)
{
if (panning) {
panning = false;
currentOffsetX = newOffsetX;
currentOffsetY = newOffsetY;
}
else if (zooming) {
zooming = false;
currentOffsetX = newOffsetX;
currentOffsetY = newOffsetY;
currentWidth = newWidth;
currentHeight = newHeight;
currentContinuousZoom = newContinuousZoom;
}
}
function touchCancel(event)
{
alert("cancel fired!");
if (panning) {
panning = false;
}
else if (zooming) {
zooming = false;
}
}
================================================== index.html: ==================================================
<!DOCTYPE html>
<html>
<head>
<title>TM Mobile</title>
<meta name="viewport" content="width=device-width height=device-height initial-scale=1 user-scaleable=no" />
<meta charset="utf-8">
<style type="text/css">
#map_canvas { height: 800px; }
#eiffelTower { position:relative; }
</style>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
<script type="text/javascript" charset="utf-8" src="simple.js"></script>
<!-- This script MUST be after $(document).bind("mobileinit", function(), so keep it last here. -->
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>
<body onload="onBodyLoad()">
<div data-role="page" data-fullscreen="true" id="map">
<div data-role="header">
<h4>Jack's</h4>
</div><!-- /header -->
<div data-role="content" id="map_canvas">
<img src='images/eiffel.jpg' id='eiffelTower'/>
</div><!-- /content -->
<div data-role="footer" data-position="fixed" >
<h4>Simple Demo</h4>
</div><!-- /header -->
</div><!-- /page -->
</body>
</html>
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com
To compile in the cloud, check out build.phonegap.com
<!DOCTYPE html>
<html>
<body onload="load_url();">
<center>
URL:<input type="text" id="theurl" value="https://i.imgur.com/VkeTnfu.jpg">
<button id="update_button" onclick="load_url()">Load URL and Reset</button><br><br>
<canvas id="mainCanvas" width="144" height="168" style="border:1px solid #d3d3d3;">Your browser does not support the HTML5 canvas tag.</canvas>
</center>
<script>
var image_x = 0, image_y = 0;
var zoom = 1;
var mouse_x = 0, mouse_y = 0, finger_dist = 0;
var source_image_obj = new Image();
source_image_obj.addEventListener('load', function() {reset_settings();}, false); // Reset (x,y,zoom) when new image loads
function load_url() {
source_image_obj.src = document.getElementById("theurl").value; // load the image
}
function update_canvas() {
var mainCanvas= document.getElementById("mainCanvas");
var mainCanvasCTX = document.getElementById("mainCanvas").getContext("2d");
var canvas_w = mainCanvas.width, canvas_h = mainCanvas.height; // make things easier to read below
// Keep picture in bounds
if(image_x - (canvas_w * zoom / 2) > source_image_obj.width ) image_x = source_image_obj.width + (canvas_w * zoom / 2);
if(image_y - (canvas_h * zoom / 2) > source_image_obj.height) image_y = source_image_obj.height + (canvas_h * zoom / 2);
if(image_x + (canvas_w * zoom / 2) < 0) image_x = 0 - (canvas_w * zoom / 2);
if(image_y + (canvas_h * zoom / 2) < 0) image_y = 0 - (canvas_h * zoom / 2);
// Draw the scaled image onto the canvas
mainCanvasCTX.clearRect(0, 0, canvas_w, canvas_h);
mainCanvasCTX.drawImage(source_image_obj, image_x - (canvas_w * zoom / 2), image_y - (canvas_h * zoom / 2), canvas_w * zoom, canvas_h * zoom, 0, 0, canvas_w, canvas_h);
}
function reset_settings() {
image_x = source_image_obj.width / 2;
image_y = source_image_obj.height / 2;
zoom = 1;
update_canvas(); // Draw the image in its new position
}
document.addEventListener('wheel', function(e) {
if(e.deltaY<0){
zoom = zoom * 1.5;
} else {
zoom = zoom / 1.5;
}
update_canvas();
}, false);
document.addEventListener('mousemove', function(e) {
if(e.buttons>0) {
window.getSelection().empty();
image_x = image_x + zoom * (mouse_x - e.clientX);
image_y = image_y + zoom * (mouse_y - e.clientY);
}
mouse_x = e.clientX; mouse_y = e.clientY; // Save for next time
update_canvas(); // draw the image in its new position
}, false);
function get_distance(e) {
var diffX = e.touches[0].clientX - e.touches[1].clientX;
var diffY = e.touches[0].clientY - e.touches[1].clientY;
return Math.sqrt(diffX * diffX + diffY * diffY); // Pythagorean theorem
}
document.addEventListener('touchstart', function(e) {
if(e.touches.length > 1) { // if multiple touches (pinch zooming)
finger_dist = get_distance(e); // Save current finger distance
} // Else just moving around
mouse_x = e.touches[0].clientX; // Save finger position
mouse_y = e.touches[0].clientY; //
}, false);
document.addEventListener('touchmove', function(e) {
e.preventDefault(); // Stop the window from moving
if(e.touches.length > 1) { // If pinch-zooming
var new_finger_dist = get_distance(e); // Get current distance between fingers
zoom = zoom * Math.abs(finger_dist / new_finger_dist); // Zoom is proportional to change
finger_dist = new_finger_dist; // Save current distance for next time
} else { // Else just moving around
image_x = image_x + (zoom * (mouse_x - e.touches[0].clientX)); // Move the image
image_y = image_y + (zoom * (mouse_y - e.touches[0].clientY)); //
mouse_x = e.touches[0].clientX; // Save finger position for next time
mouse_y = e.touches[0].clientY; //
}
update_canvas(); // draw the new position
}, false);
document.addEventListener('touchend', function(e) {
mouse_x = e.touches[0].clientX; mouse_y = e.touches[0].clientY; // could be down to 1 finger, back to moving image
}, false);
</script>
</body>
</html>