Giter VIP home page Giter VIP logo

img-touch-canvas's Introduction

ImgTouchCanvas

Add touch gestures (pinch zoom and touch drag) to an image (like Google Maps).

Based on a canvas element for smooth rendering.

Plain HTML5 / vanilla JS, no external libraries needed.

Tested in Chrome 28, Firefox 21, Android Browser 4.2.2 and Firefox for Android 18

Usage

See a live example here : http://www.rombdn.com/img-touch-canvas/demo

Define a container in which the image will be able to be resized and moved, then add a canvas element.

The image will be scaled to cover all the container so if you want the image to be showed at its original size by default then set the container size to match the image original size (see example).

<html>
<body>
    <div> <!-- set desired position and size to that div -->
        <canvas id="mycanvas" style="width: 100%; height: 100%"></canvas>
    </div>

    <script src="img-touch-canvas.js"></script>
    <script>
        var gesturableImg = new ImgTouchCanvas({
            canvas: document.getElementById('mycanvas'),
            path: "your image url"
        });
    </script>
</body>
</html>

Licence

(c) 2013 Romain BEAUDON This code may be freely distributed under the MIT License

img-touch-canvas's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

img-touch-canvas's Issues

Sync two img-touch-canvas

I have two img-touch-canvas in different browsers displaying the same image (I've synchronized it through websockets). Aslo, both img-touch-canvas are using the canvas to display their contents (by the same I mean the same size). When the user zooms/moves the image on one client, I want to sync this in the other one. I am doing so with:

gesturableImg.position.x = data.position_x;
gesturableImg.position.y = data.position_y;
gesturableImg.scale.x = data.position_x;
gesturableImg.scale.y = data.position_y;

gesturableImg.animate();

The problem is that the canvas become fully grey and I lost everything I had on it. Is this the right way to sync to img-touch-canvas?

Using toDataURL makes imgtouchcanvas irresponsive

When using toDataURL as here: setInterval(function(){document.getElementById("img_canvas").toDataURL()}, 1000); makes imgtouchcanvas irresponsive in the millisecond that toDataURL is called.

If the user is touching the canvas (zooming, moving) in the exact time that toDataURL is called then it has to stop touching and start touching again to regain control.

Performance Issue in iOS

When I load more than one image with this Library it causes the iPhone's CPU to over-clock. Is there a way to clear the global object or null the canvas?

Rendering this canvas on a retina screen looks pixely

I'm wondering if you have any suggestions for me- I'm rendering an image on a 360x360 square and displaying it on an iphone. However, the images look quite low quality- I assume because of the scaling functions being used.

Do you suggest any changes to the library or approaches that will let me retain a high quality image while using your cropping library?

没有任何操作,函数也会一直运行

我在下面打了个console,就一直在运行 console.info(this,888);

ImgTouchCanvas.prototype = {
animate: function() {
//set scale such as image cover all the canvas
if(!this.init) {
if(this.imgTexture.width) {
var scaleRatio = null;
if(this.canvas.clientWidth > this.canvas.clientHeight) {
scaleRatio = this.canvas.clientWidth / this.imgTexture.width;
}
else {
scaleRatio = this.canvas.clientHeight / this.imgTexture.height;
}

                this.scale.x = scaleRatio;
                this.scale.y = scaleRatio;
                this.init = true;
            }
        }

        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        console.info(this,888);

Image flickers around

It seems that img-touch-canvas fills the canvas based on the image's width. If the canvas has a fixed size then some blank space is left on the bottom of the canvas. The problem is that zooming the image until it fills the whole canvas makes the image flickers around.

Perhaps zooming the image in the initialization to fit the whole canvas would be one way to avoid that. What should I do to get the image zoomed to fit the canva's area in the initialization?

Adding image rotation

Hi, I'm having some issues wile adding rotation to the canvas image. This is the code I'm using but after I rotate the image on a mobile device, it freezes after the rotation happens. What would be the best way to implement that into your script?

On the mobile device I'm presenting two buttons to rotate the image (right or left)

var canvas = document.getElementById('canvas'),
context = canvas.getContext("2d"),
image = "temp.png";

function drawRotated(degrees){
context.clearRect(0, 0, canvas.width, canvas.height);

// save the unrotated context of the canvas so we can restore it later
// the alternative is to untranslate & unrotate after drawing
context.save();

// move to the center of the canvas
context.translate(canvas.width / 2, canvas.height / 2);

// rotate the canvas to the specified degrees
context.rotate(degrees * Math.PI / 180);

// draw the image
// since the context is rotated, the image will be rotated also
context.drawImage(image,-image.width / 2, -image.width / 2);

// we’re done with the rotating so restore the unrotated context
context.restore();
}

thanks for your help in advance!
Alvin.

Rotate Image/Canvas

Hi,
thank for this great script! i just cant find a way to rotate the image/canvas without mirroring the touch controls. can you give me a tip?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.