Manipulate image position with touchmove event using CSS3 transformations on an iPad

Looking to adjust the position of an image on my iPad3, but running into some difficulties.

The movement isn't as smooth as desired and seems to lag behind the gestures being made.

This is what I have so far (a 3MB base64 image)

<img src="data:image/jpeg;base64,/9j/4AAQ....">

CSS:

img {
    transform: translate3d(0,0,0);
}

And here's the JavaScript:

img.on('touchstart', function (e) {
     var diffX = e.pageX;
     var diffY = e.pageY;
     img.on('touchmove', function (e) {
         translateX  += e.pageX - diffX;   
         translateY += e.pageY - diffY; 

         img.css({'-webkit-transform': 'translate(' + translateX + 'px, ' + translateY + 'px)'});

         diffX = e.pageX;
         diffY = e.pageY;

     });             
     return false;
})
...

This setup works well on my laptop, however, not on my iPad3. I've also experimented with requestAnimationFrame, but it only updates once I stop moving. Any suggestions for improving performance on my iPad?

Answer №1

Most of the mathematical calculations are performed outside of the touchmove function in this code snippet. It directly sets the webkittransform without going through a lengthy jQuery CSS function. Additionally, it utilizes the 3D transition feature which may not be effectively implemented by simply adding translate3d in the CSS file for iOS7.

var sx, sy, cx=0, cy=0, img=document.images[0];
img.addEventListener('touchstart', ts, false);
img.addEventListener('touchmove', tm, false);
img.addEventListener('touchend', te, false);

function ts(e){
 e.preventDefault();
 sx = e.pageX - cx;
 sy = e.pageY - cy;
}

function tm(e){
 this.style.webkitTransform = 'translate3d(' + (e.pageX - sx) + 'px,' + (e.pageY - sy) + 'px,0)';
}

function te(e){
 cx = e.pageX - sx;
 cy = e.pageY - sy;
}

In addition, even if you own an iPad 3, it may struggle to handle large images. To optimize performance, consider enclosing everything within a div element with overflow:hidden property set.

div {
 width:400px; /*consider trying 100%*/
 height:400px; /*consider trying 100%*/
 overflow:hidden;
}

For a mouse move example (similar to touch), you can refer to this JSFiddle demo: http://jsfiddle.net/AtBUh/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In what way does Google+ make their logo come to life on the left side of the navigation bar when the scrollbar is minimized?

I'm curious about the animation Google+ uses to make their logo slide in on the navigation bar when it shrinks. I've managed to shrink the scrollbar on scroll, but I can't quite replicate their technique for animating the icons. I'm eag ...

Changing the background color of a PHP input based on the webpage being viewed - here's how!

I'm in the process of creating a website where each page will have its own unique background color. Additionally, I am using a PHP input for both the header and footer sections, which need to change their background colors based on the specific webpa ...

Increase the value of a property within an array of objects based on certain conditions

My task involves incrementing the rank value by one until the name property changes. I am utilizing the page and rank properties to track when this change occurs. In addition, I want to increment it once whenever the type is not equal to none, and then re ...

Remove elements from an array in Typescript if they are not present in a nested array

I am struggling with filtering values from a single-dimensional array based on id matches in an array of objects. array1 = [1, 3, 15, 16, 18]; array2 = [ { id: 1, dinner : pizza }, { id: 15, dinner : sushi }, { id: 18, dinner : hummu ...

Foundation 5.2.2: Creating a Dropdown Menu

Would it be possible to achieve this effect using Foundation: https://i.stack.imgur.com/UyYqK.png ? I am interested in implementing 2 COLUMNS in the TopBar Menu: foundation.zurb.com/docs/components/topbar.html I came across a MegaMenu (codepen.io/winghou ...

Refreshing CSS styles following adding content with jQuery

When I load a DIV with content from a callback that contains multiple HTML elements, each with their own associated classes, the styles are not appearing correctly after using .append. $.ajax({ type: "GET", url: "blah.xml", dataType: "xml", ...

clear JavaScript array of elements

I've been grappling with this issue for hours now, and it seemed so straightforward at first; My goal with JavaScript is to iterate through an array, retrieve the current index value, and then remove that value from the array. I've read that spl ...

What makes CSS Overflow: hidden toggle from working initially to suddenly not working?

There seems to be an issue with the code below - it works fine initially, but as soon as I tweak the height values for .Image, it stops functioning as expected. The test image starts overflowing instead of staying hidden, and no matter how many times I a ...

Dynamically populate an HTML table/grid in CRM 9 with data retrieved from JavaScript records

Currently, I am developing an HTML Page within Dynamics CRM 9.0 environment. A Webresource JavaScript has been created and linked to the HTML page. There is a specific function in the JavaScript that retrieves data from the CRM entity using the following c ...

What is the technique for anchoring elements at the top of the page when scrolling?

There is a common design feature where the sidebar on the left starts at a lower position on the page, but as you scroll it moves up to the top and remains fixed in place instead of disappearing off the screen. This is a popular design trend that I have ...

Updating the Ajax Url dynamically while scrolling

I am trying to update the Ajax URL dynamically, and here is my progress so far: var size=1; var from = 1; window.addEventListener('mousewheel', function(e){ if(e.wheelDelta<0 && from<5){ from++; } else if(e.whe ...

Utilize JavaScript to execute postback calls

Currently, I am working on a project using asp.net MVC5. Within my HTML code, I have the following row: <input id="setCulture" type="hidden" name="culture" value="" /> Accompanied by this JQuery row: $('#setCulture').parents("form"). ...

In the realm of ASP.NET, the Razor FormExtensions.BeginForm Method holds the

In my current view, there is a dropdown list that allows the user to select an option which will then redirect them to another page or controller. I am using JavaScript to retrieve the selected value from the dropdown list, but I am facing difficulties whe ...

Create a dialog box with rounded triangle corners

In the process of developing an application, we are incorporating a text display feature within a div that resembles a chat box direction, but with a curved style as depicted in the screenshot linked below. Desired design exemplified in purple Target des ...

Issue with focus transition animation not functioning properly when unfocusing an HTML input field

Hey there! I'm currently working on styling a basic input, and I'm having trouble getting the unfocus animation to work properly. Can anyone help me figure out what's going wrong? I've tried commenting out the all: unset line and expl ...

Unable to deactivate button within component using setState is ineffective

Once the button within the RecipeListItem component is clicked and the handleFavorites function has been triggered, I want the button to become DISABLED. What am I missing in my logic? Because this code isn't functioning as expected... Child compone ...

"Efficiently incorporating a responsive sprite background image on your website -

Hey there! I am dealing with a situation where I have two columns of content in a container. The first column contains text, and the second column is a span with a background sprite image. The issue arises when the screen resolution gets smaller - I want ...

Using Angular JS, filter the ng-repeat to only display items that have a specific property

I have a data file that contains keys such as: [ { "message": "Verify envelopes are properly loaded.", "hasFigure": false, "figureX": 0, "figureY": 0 }, { "message": "Ensure the paddle is in the down position.", "hasFigure": true, "figureX ...

The additional cost associated with using a React hook is called the "

Our system includes a theme context provider that passes down a theme to all child components, calculated based on the device's dimensions. We can easily access these values using the useTheme hook in any component. In addition, we have a constants f ...

Could use some help with configuring express routes with parameters

Greetings! I am new to working with Express and I am currently in the process of creating a portfolio website. In my project, I have a Pug file named project.pug which includes HTML content that should dynamically render based on the ID of each project sto ...