davidenke / context-filter-polyfill Goto Github PK
View Code? Open in Web Editor NEWPolyfills `CanvasRenderingContext2d.filter` capability of adopting CSS3 filters to canvas contexts at least partially.
License: MIT License
Polyfills `CanvasRenderingContext2d.filter` capability of adopting CSS3 filters to canvas contexts at least partially.
License: MIT License
Without this polyfill, globalCompositeOperation
works in a 2d canvas on iOS 16, while filter
does not.
With this polyfill, globalCompositeOperation
does not work at all, while filter does work as expected on iOS - although it possibly leaks memory - iPhone gradually gets very hot.
If there's another solution that gets both to work on iOS, I'd be open to try it, but in its current state, this polyfill is a no go. Any plans to update it?
the ctx.clearRect method doesn't clear
https://jsfiddle.net/zakariamouhid/4wpmb3ut/2/
should simply call the original clearRect
context-filter-polyfill/src/patches/method.patches.ts
Lines 22 to 25 in 17e4a91
// do not apply on mirror
if (this.canvas.__skipFilterPatch || member === 'clearRect') {
return original.call(this, ...args);
}
I try to install the plugin in vue.js/typescript project using NPM, but the result of the install is just the dist/ folder and index.js.
How do we properly install and setup the plugin for use?
Just asking if there's an interest of adding support to url custom filter here
It changes the default CanvasRenderingContext2d and therefore while adding text to canvas the method does not work properly.
Can you please help fix this?
Thanks in advance.
In Safari version 12.1.1 (14607.2.6.1.1) in MacOS 10.14.5 this module draws a partial rectangle of the whole image on the canvas with the filter applied. Removing the ctx.filer = ... renders the whole image again. In Chrome or Firefox there is no issue.
require('context-filter-polyfill');
const drawImagesInCanvas = (canvas, imageDescriptors, includeAll = true) => {
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.filter = 'contrast(1.4) sepia(1) drop-shadow(9px 9px 2px #e81)';
for (const imageDesc of imageDescriptors) {
if (!includeAll && !imageDesc.include) { continue; }
const { image, rotation } = imageDesc;
const size = imageDesc.resize ? imageSizeThatFitsInSize(image, new Size(canvas.width, canvas.height)) : new Size(image.width, image.height);
if (rotation > 0) {
// save the unrotated context of the canvas so we can restore it later
// the alternative is to untranslate & unrotate after drawing
ctx.save();
// move to the center of the canvas
ctx.translate(canvas.width / 2, canvas.height / 2);
// rotate the canvas to the specified degrees
ctx.rotate(rotation * Math.PI / 180);
ctx.drawImage(image, size.width / -2, size.height / -2, size.width, size.height);
// we’re done with the rotating so restore the unrotated context
ctx.restore();
} else {
ctx.drawImage(image, 0, 0, size.width, size.height);
}
}
};
Screenshot in Chrome: http://www.isaacrivera.com/canvas/chrome.png
Screenshot in Safari: http://www.isaacrivera.com/canvas/safari.png
Please advise!
https://jsfiddle.net/zakariamouhid/4wpmb3ut/
set the shadowContext canvas size
shadowContext.canvas.width = context.canvas.width;
shadowContext.canvas.height = context.canvas.height;
Thank you for the polyfill!
Just a minor bug, when filters doesn't match it fails with cannot use .map of undefined
, while in normal browser works fine
Hello,
I was using this with CropperJs to edit images in IOS safari, but after using CropperJs crop() function this canvas seems to stay cropped for the next time I load a image into CropperJs. Do you know any fix for this?
Thanks!
Hi,
Thanks for your code it's very usefull, however I can't use it with picajs.
In my app I use pica js for resizing pictures, when I use your polyfill I have an error:
[Pica: cannot use getImageData on canvas, make sure fingerprinting protection isn't enabled]
Internally pica use this code to check if browser block fingerprinting with getImageData
function can_use_canvas(createCanvas) {
var usable = false;
try {
var canvas = createCanvas(2, 1);
var ctx = canvas.getContext('2d');
var d = ctx.createImageData(2, 1);
d.data[0] = 12;
d.data[1] = 23;
d.data[2] = 34;
d.data[3] = 255;
d.data[4] = 45;
d.data[5] = 56;
d.data[6] = 67;
d.data[7] = 255;
ctx.putImageData(d, 0, 0);
d = null;
d = ctx.getImageData(0, 0, 2, 1);
if (d.data[0] === 12 && d.data[1] === 23 && d.data[2] === 34 && d.data[3] === 255 && d.data[4] === 45 && d.data[5] === 56 && d.data[6] === 67 && d.data[7] === 255) {
usable = true;
}
} catch (err) {}
return usable;
};
function createCanvas(width, height) {
var tmpCanvas = document.createElement('canvas');
tmpCanvas.width = width;
tmpCanvas.height = height;
return tmpCanvas;
}
can_use_canvas(createCanvas)
Can you help me with this issue I really would like to use your polyfill with picajs
When I try to draw on my canvas, where each touchmove
event calls ctx.lineTo
and ctx.stroke
, only a stroke is drawn for the first point. I assume this is because the context of where the path is, is lost after the drawing function stroke
is called? But maybe I missing something else?
I was able to get the drawing working again by calling ctx.moveTo
and manually setting to the previous point before each ctx.lineTo
call. However, for other reasons this workaround didn't solve this issue for me.
After looking at your code a bit, I set Object.defineProperty(canvasElement, '__skipFilterPatch', { value: true })
on my drawing canvas because I don't need the ctx.filter
on this particular canvas at all, I just use it with another canvas. This solves it for me at least. On the other canvas your polyfill works really well, thank you very much for this library!
Howdy, I could not figure out the right way to use this polyfill in an npm project (simple create-react-app
for example).
Eventually, I copied the contents node_modules/context-filter-polyfill/dist/index.js
into a <script>
tag in my head, and this worked but feels like the wrong way to do it.
What's the correct way?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.