Problem with off-screen canvas performance in Firefox

Currently, I am facing an issue that I suspect is due to a mistake on my end, but I can't seem to pinpoint it. (This issue arises in Firefox 8)

Here's what I'm doing - I have a large sprite file, from which I extract a single tile and place it on a small canvas. Then, using the basic form of `drawImage`, I draw this isolated tile in multiple locations on my main "screen" canvas.

Instead of utilizing the last form of `drawImage` which allows for selecting only a portion of the larger sprite file, I have observed a surprising performance boost of around 10% in Chrome by avoiding this clipping method. However, in Firefox, the frame rate drops drastically from 300 to 17 FPS.

Based on my observations, it seems that transferring images directly to the canvas is significantly faster than copying images between canvases in Firefox, with a speed difference of approximately 20 times.

Is my understanding correct? I haven't come across any specific information regarding this scenario, but this is what my testing has revealed.

Below is the code snippet I am currently working with. Could there be something fundamentally wrong with it?

function Test5() {
    var imgSprite = $('imgSprite');
    var tileCanvas = document.createElement("canvas");
    tileCanvas.width = 64; tileCanvas.height = 31;
    var tileCtx = tileCanvas.getContext("2d");
    tileCtx.drawImage(imgSprite, 0, 0, 64, 31, 0, 0, 64, 31);

    //--------------------------------------
    var ctx = getContext('mainScreen');

    ctx.fillStyle = '#fff';

    time(function() { // time will run this function many times and time it
        ctx.fillRect(0,0, 1200,900);
        var x=0, y=0, row = 0;
        for (var i=1; i < 1000; i++) {
            ctx.drawImage(tileCanvas, x,y); 
            // some simple code to calculate new x/y
        }
    }, 1000, "Test5", 'Drawing from an individual tile canvas, instead of a section of big sprite');
}

If, instead of

 ctx.drawImage(tileCanvas, x,y);  

I do :

 ctx.drawImage(imgSprite, 0, 0, 64, 31, x, y, 64, 31);  

The latter approach is 20 times quicker.

Am I overlooking something significant here?


EDIT: Following this query, I created a test page for myself to experiment with various methods on different platforms and determine the most efficient approach for each.

Please disregard the messy code layout in the link, as it was put together quickly and not intended for public viewing, even by myself after a few days.

Answer №1

It's difficult to say for certain without profiling, especially considering your specific images and possibly your specific graphics card and driver. However, it is possible that you are encountering an issue related to this bug

This particular bug could potentially result in a slower drawImage function when using the canvas argument, although a 20x slowdown seems quite surprising (hence the uncertainty).

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

Retrieving AngularJS scope variables using jQuery

Is there a way to retrieve an Angular form variable within jQuery code? I have a controller set up for an HTML page where, upon clicking a button, I invoke a jQuery function. How can I access a specific form variable within this jQuery method? Specificall ...

What could be the reason for the failure of the .is(":hover") method?

Here is some code I'm using to fade out certain elements on a webpage if the mouse hasn't moved for a period of time: idleTime = 0; var idleInterval = setInterval(function() { idleTime++; if (idleTime > 1) { var isHovered = $ ...

Handling Removal of Selected Option in React Material-UI Autocomplete Single Selection

I am currently using material UI autocomplete to create a single-select dropdown. However, I have encountered an issue wherein the onChange event does not get triggered when I click the close button on the right side of the input. This prevents my state fr ...

Javascript is experiencing a decrease in the variability of its content

I currently have multiple pages structured like this: <body> <table><tr><td align="center" width="100%"> --PAGE HTML-- </td></tr></table> </body> For a temporary period, I need to change the str ...

Personalize Your Highcharts Legend with Custom HTML

Currently, I am working on a project with a highcharts graph where I need to gather data from the user regarding each line displayed. My goal is to include a text input box next to each label in the legend that relates to the series name. Once the user ent ...

Ways to convert all keys to uppercase in an array of objects?

Is there a way to capitalize the first letter of every key in an array of objects? I attempted to achieve this with the code below, but it's not working as expected. Any suggestions or corrections are appreciated. #current code function capitalizeO ...

include a new value to the current state array within a react component

I am working with an array in React State and I need to add a new property called Value. However, every time I try to add it, it creates a new item in the array. What I actually want is for the new property to be added as follows: value: '' Belo ...

Creating a 2D Image Display in three.js

I'm facing a challenge with my threejs project. My goal is to have a 2D image appear on the screen when I press a key. I've done some research but haven't been able to find a solution that works for me. The methods I've tried either don ...

NextJS displays outcomes as per query parameters obtained from an external API

I have set up my NextJS app to connect to a rest API. The API returns results based on different filters in the query string, such as: /jobs /jobs?filter=completed /jobs?filter=upcoming /jobs?filter=cancelled On my NextJS page, I have a few buttons that I ...

Showing outcomes from various API requests

I have encountered an issue while making two API calls to a backend server and trying to display both responses in JSON format to the user. Strangely, alert 2 is showing as 'undefined' when I check the console, while alert 1 works perfectly fine. ...

Eliminate the array from the data retrieved through an http request in AngularJS

Currently, I am making an http call to retrieve data from a database. This process involves calling 6 different types individually. $scope.getAll = function() { var url = 'http://someurl/'; var allObjects = []; $sc ...

What is the process for implementing CSS on the header of a Material UI Table with stickyHeader functionality?

I've implemented a Material UI table with the stickyHeader prop to keep the header fixed. However, I'm facing an issue where other CSS styles are not being applied to the header section. const useStyles = makeStyles((theme) => ({ tableHead ...

The "Overall Quantity" of items will vary as it goes through different numerical values, despite the fact that I employed --

I am currently working on an e-commerce website with a shopping cart feature. The cart displays the number of items added to it, which increases by one when 'Add to Cart' is clicked and decreases by one when 'Remove' is clicked. However ...

Can you please explain how to separate a collection of emails that have various formats using JavaScript?

My list of emails contains two formats: with name name <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395c5458505579585d5d4b5c4a4a175a5654">[email protected]</a>> without name <a href="/cdn-cgi/l/email-prote ...

Exploring Angular component testing through jasmine/karma and utilizing the spyOn method

I have been facing an issue while trying to test my component. Even though the component itself works perfectly, the test keeps generating error messages that I am unable to resolve. Here is the snippet of code that I am attempting to test: export cl ...

Triggering an event for a div using Javascript, along with a Rails loop

I have a page showcasing a collection of listings that are displayed on the main page. <% @listings.each do |listing| %> # do something <% end %> Each listing now includes a data-toggle, essentially a button. <a id="chat-menu-toggle" h ...

Implementing automatic selection mode in Kendo MVC grid

Seeking to modify the SelectionMode of a Kendo MVC Grid, I aim to switch from single to multiple using Javascript or JQuery upon checkbox selection, and revert back when the checkbox is unchecked. Is this feasible? Additionally, I am successfully binding a ...

Reordering div elements with JQuery

As soon as my page loads, I have a div set to display:block and another div set to display:none. I have implemented a toggle switch that should replace the visible div with the hidden one. However, I am facing an issue where after performing the switch, ...

Converting JavaScript numbers into years and months format

Given an integer, for example 20, I am trying to calculate how many months and years are represented by that number. For 20, the result would be 1 year and 8 months. How can this be achieved using JavaScript? switch (props.term) { case (props.term ...

Verifying the visibility of an element

I am facing a challenge with a list of apps displayed on a non-angular page. The availability of these apps depends on the subscription level purchased by the user. Even if an app has not been purchased, it is still listed but displayed with an overlay (pl ...