GithubHelp home page GithubHelp logo

pixelscommander / canvasimage Goto Github PK

View Code? Open in Web Editor NEW
34.0 6.0 3.0 1.1 MB

Converts image to canvas with same look. Useful for memory optimization in case of responsive image usage. Also is good for solving PhoneGap Android anti aliasing issue.

JavaScript 100.00%

canvasimage's Introduction

CanvasImage

Converts image to canvas with same look.

Useful for memory optimization in case of responsive images usage. Also is good for solving Android antialiasing issue in PhoneGap or web applications.

Usage

Simpliest way to use CanvasImage is by calling new CanvasImage('selector') or new CanvasImage(DOMElement)

There is also approach to convert all images on current page. For this use CanvasImage.all()

###Memory consumption optimization use case###

#####Problem#####

Responsive images are common citezens of web applications and very often they are a bit larger than we need for particular screen size. This creates memory overusage problem for mobile devices.

For example you have universal web app which works on desktops as well as on iPhones.

image

Problem is that iPhone is still forced to keep large desktop image in memory.

#####Solution####

Most obvious way to fix this is to create few sets of images for different screen sizes however this is not easy to do - you will need additional files to support, styles to develop and bunch of time to spend. Here CanvasImage is a game changer.

How it works:

  • Library finds all img tags on page
  • Replaces them with canvas tags of same size
  • Draws images content to canvases reducing size to needed one
  • Removes old big images from memory

After this we have set of canvases which look absolutely like images we had before but with one difference - we removed all image data which is not needed for our current window size.

image

###Android anti-aliasing issue use case###

#####Problem#####

A lot of Android devices have very annoying browser behaviour - it disables antialiasing on every touch to optimize rendering. This creates some kind of blinking effect when user touches screen.

#####Solution#####

It is possible to avoid such a problem with disabling browser anti-aliasing with CSS so there will be nothing to disable then. Have sense?

Let`s add next CSS code to stylesheet:

	* {
		image-rendering: optimizeSpeed;
		image-rendering: -moz-crisp-edges;
		image-rendering: -o-crisp-edges;
		image-rendering: -webkit-optimize-contrast;
		image-rendering: optimize-contrast
		-ms-interpolation-mode: nearest-neighbor;
	}

Yes, this will work until we do not have responsive images in app. Since we will start to use them - issue will appear again. This happens because of difference between antialiasing mechanisms of CSS engine and Android WebView component. CSS antialiases images before they were scaled and browser antialiasing is being applied after scaling.

To avoid this effect you have to have images absolutely unscaled somehow. Here CanvasImage can be best approach possible beacuse it replaces scaled images with unscaled canvases.

canvasimage's People

Contributors

pixelscommander avatar

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

nask0 seco

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.