photon
fabric.js
Our great sponsors
photon | fabric.js | |
---|---|---|
6 | 49 | |
2,481 | 27,470 | |
- | 1.5% | |
6.7 | 9.3 | |
5 days ago | about 19 hours ago | |
Rust | JavaScript | |
Apache License 2.0 | GNU General Public License v3.0 or later |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
photon
-
Show HN: OS Image processing API running on edge functions using Rust and WASM
Image processing/transformations with built-in CDNs is one of the more common solutions you would find out there but can get pretty expensive and locked in over time.
Vercel is a good example that provides a built-in “next/image” solution for the NextJS framework that optimises images for better performance, however, it gets very expensive as you scale.
Alternatively, running your own service with one of the most common image processing libraries like SharpJS is great but can get pretty difficult to run in a serverless environment with CDNs and all the other bells and whistles alongside all your other code while the cold startup time of Sharp in a serverless environment can be pretty brutal.
With all the growth around rebuilding engines in Rust for performance gains, I thought there must be an image processing library written in Rust which could be compiled to WebAssembly (WASM) and run way faster. That’s when I discovered Photon https://github.com/silvia-odwyer/photon by Silvia, who has done amazing work with the library.
This eventually led me to discover https://github.com/fineshopdesign/cf-wasm/tree/main/packages... that has already transcribed the Photon package into a WASM binary for easy use in edge v8 environments like Cloudflare workers or NextJS Edge functions.
Using these packages I built an API layer on the NextJS framework allowing you to easily process local and remote images at blazing fast speeds with a bunch of helper functions making it similar to use as most paid Image processing APIs. If hosted on Vercel, you can make use of the built-in CDN cache for files less than 10 MB at no extra charge with Edge caching.
Some of the features:
- Blazing fast image processing
- Fully serverless, runs on Vercel Edge functions
- Resize, crop, compress, tint, rotate, format and more
- Global distribution with Edge functions
- Automated CDN cache with Edge functions
- Replacement for `next/image` processing on Vercel
- Local and remote image processing
Some problems:
- The Photon lib has last been updated in 2020
- There are still a few obvious bugs when using it for more advanced requirements like 8k image processing on edge function due to poor memory management in Rust
- No sexy features right now like AI image optimisation and selective background blurring
- If you are looking for an all-in-one solution with Image Storage + image processing/CDN, this is not it (something we will be releasing soon on JigsawStack.com)
I think the Photon library and the API have a lot more potential for growth and will be hoping for more contributions from the community as Rust gets more popular!
-
Building an online image compressor
My first implementation was based on photon_rs, a Rust based web assembly library for image processing. It worked pretty well, but it was slower than OP website. Without digging much it seems logical since photon isn’t optimized for performance.
- Rust/WebAssembly image processing library
-
Image Compression Library
What you describe reminded me of photon. Check out the website. If there isn't an overlap of functionality then you can definitely integrate it into your own site.
- Off my chest: When are some of you browser devs gonna give us native image scaling? This is the best we got ATM.
-
Monitoring UX in Single Page Applications
For the image, you are able to either use your webcam to take a photo, or upload a photo of your own before moving onto the filters. Image filtering is included in most image manipulation libraries. I had used ImageMagick before, but had a terrible time with it. I wanted to find a better way to do Instagram-like filtering. I came across Photon, a high performance image processing library written in Rust and can be compiled to WebAssembly, and there was already a React demo so it was easy to integrate. Once you choose the filter you want, you move onto the decoration stage.
fabric.js
-
Fun, Beautiful, Printable 'Story Cards' for Kids with Cloudflare AI
This AI-powered Story Card Maker is built as a SvelteKit application with Typescript. Using Flowbite Svelte component library, the whole application was laid out. The layout for the Story Card (emulating the size of a postcard - 4" x 3") is created as an HTML Canvas using Fabric.js.
- JSON Canvas – An open file format for infinite canvas data
-
Step by step on how to setup fabric.js in the next.js app
Fabric.js is a powerful and simple Javascript HTML5 canvas library. It provides an interactive object model on top of the canvas element. Fabric also has SVG-to-canvas (and canvas-to-SVG) parser.
-
How I choose Fabric.js again
Based on this, I found that some of the libraries are dead and no longer have any support. Only two libraries are still alive and have significant amount of stars on GitHub and downloads on NPM. They are Fabric.js and Konva.js.
-
Building application Like Canva in ReactJS
I'm actually in the process of building something similar. It's a React app and I'm using the FabricJs library
-
How can I build a configurator like this ?
I've played with this tech in early 2010s - they are using HTML5 Canvas tech + something like Fabric.js(http://fabricjs.com/) that controls Canvas objects.
-
Canvas Element with multiple images that can be moved?
this library might help you: http://fabricjs.com/
-
How would I wrap an image on an object in web?
I did some research and the most promising result was using Fabric.js (http://fabricjs.com/) to put images on products. I may not be experienced enough in JS to understand the full capabilities of this library, but it looks like most of their stuff works on flat surfaces. I am trying to replicate the properly wrapped/textured affect that is achieved in the blanket below.
-
Any ideas on how to build this floating sidebar and how it functions?
set up a canvas drawing & rendering framework, for example http://fabricjs.com/
-
Image Manipulation with Fabric.js and Canvas
body{ display:flex; flex-direction:column; align-items:center; }
Learning Fabric
Enter fullscreen mode Exit fullscreen modeNext, we initiate a Canvas instance in our script tag:
Syntax
new fabric.Canvas(element: HTMLElement|String, options: Object)
Enter fullscreen mode Exit fullscreen modeThis allows Fabric.js to create this class instance so that we can use the provided methods of Fabric.js:
const canvas = new fabric.Canvas("canvas", {})
Enter fullscreen mode Exit fullscreen modeNow, we’ll go ahead and give our canvas different properties, such as width, height, and background color:
const canvas = new fabric.Canvas("canvas", { width:500, height:500, backgroundColor:green })
Enter fullscreen mode Exit fullscreen modeOutput
Creating a Rectangle with Fabric.js
Now that we’ve initialized a canvas and given it various properties, we’re going to create a rectangle using the Fabric.js object fabric.Rect:
Syntax
new fabric.Rect({object})
Enter fullscreen mode Exit fullscreen modeHere the object houses the rectangle properties:
const canvas = new fabric.Canvas("canvas", { width:500, height:500, backgroundColor:green }); const rectangle = new fabric.Rect({ width:200, height:200, /* fill is used to define the background color*/ fill:'red' }) /*the add method is used to add objects to the canvas element */ canvas.add(rectangle) /*the renderAll method used to render the canvas*/ canvas.renderAll();
Enter fullscreen mode Exit fullscreen modeThe
add()
method is used to add objects to the Canvas element. In this case, it adds the rectangle we created to the canvas:Output
Creating a Circle
In the same way, we’ll create a circle. This time, we’ll use fabric.Circle:
const canvas = new fabric.Canvas("canvas", { width:500, height:500, backgroundColor:"lightgray" }); const circle = new fabric.Circle({ radius:60, fill:"blue" }) canvas.add(circle); canvas.renderALl();
Enter fullscreen mode Exit fullscreen modeOutput
With the above steps, we created a rectangle and a circle using Fabric.js. Note how interactive the objects are! Next, we’ll discuss images in Fabric.js.
Images with Fabric
The Fabric.Image is a Fabric.js object used to render images on a canvas. With Fabric.js, we can interact with the image. Images can be added to our canvas in two ways:
- Fabric.Image
- Fabric.Image.fromURL
Using the Fabric.Image constructor requires us to create an image element in our HTML and then insert it into the constructor:
Syntax
fabric.Image(image_element)
Enter fullscreen mode Exit fullscreen modeHTML
Enter fullscreen mode Exit fullscreen modeJS
const image_element = document.querySelector('#image'); const image = new fabric.Image(image_element) canvas.add(image); canvas.renderAll();
Enter fullscreen mode Exit fullscreen modeHowever, Fabric.Image.fromURL requires only the image URL (the URL can be retrieved from a website or your local folder), and is simpler than the former:
fabric.Image.fromURL(URL, function());
Enter fullscreen mode Exit fullscreen modeAfter passing the URL, the callback function after the image is loaded. The first default parameter of the callback function is the fabric.Image object. Now, we can modify the image properties and then add it to the canvas:
const canvas = new fabric.Canvas("canvas", { width:500, height:500, backgroundColor:"White" }); fabric.Image.fromURL('my_image.png',(img) => { // the scaleToHeight property is use to set the image height img.scaleToHeight(250) // scaleToWidth is use to set the image width img.scaleToWidth(250) canvas.add(img) }) canvas.renderAll()
Enter fullscreen mode Exit fullscreen modeOutput
Editing Images with Fabric.js
Now that we know how to insert an image into a canvas, we’ll look at other image objects like image filters.
Image Filters
Fabric.js provides us with built-in filters:
- BaseFilter
- Brightness
- convolute
- GradientTransparency
- Grayscale
- Invert
- Mask
- Noise
- Pixelate
- RemoveWhite
- Sepia
- Sepia2
- Tint
Every instance of
fabric.Image
has the "filters" property, which is a simple array of filters. We can use the array method to perform any required operation, including removing filters (pop, slice, shift), adding filters (push, unshift, slice), and even combining multiple filters. When we callapplyFilters
, any filter in the “filters” array will be applied one by one. Let’s use some of these filters:Syntax
new fabric.Image.filters // after that you can add any filter of your choice // Example new fabric.Image.filters.Grayscale()
Enter fullscreen mode Exit fullscreen modefabric.Image.fromURL('my_image.png', (image) => { image.scaleToHeight(300) image.ScaleToWidth(300) // Creating filter const filter = new fabric.Image.filters.Grayscale() //push the filter instance to filters image.filter.push(filter) // apply filter image.applyFilters() //add image into canvas (it also re-render the canvas) canvas.add(image) },{crossOrigin: 'anonymous'}); canvas.renderAll();
Enter fullscreen mode Exit fullscreen modeOutput
Above, we used the Grayscale filter; we can also apply other filters. All we have to do is simply change the filter, as shown in this example:
fabric.Image.fromURL('my_image.png', (image) => { image.scaleToHeight(300) image.ScaleToWidth(300) // creating filter const filter = new fabric.Image.filters.Sepia() //add filter image.filter.push(filter) // apply filter image.applyFilters() //add image into canvas (it also re-render the canvas) canvas.add(image) },{crossOrigin: 'anonymous'}); canvas.renderAll();
Enter fullscreen mode Exit fullscreen modeOutput
Image Blending
Blending is mixing two or more images to create a new image. Fabric.js gives us this ability. Here, we’re going to use the image blending filter for our image:
Syntax
new fabric.Image.filters.BlendImage({ // the image that we want to blend image:imageA, // blend mode (in this case we are using the 'add' mode) mode:'add', alpha:0.5 });
Enter fullscreen mode Exit fullscreen modefabric.Image.fromURL('my_image.png', (imageA) => { imageA.scaleToWidth(350) imageA.scaleToHeight(350) canvas.add(imageA) // Blending filter filter = new fabric.Image.filters.BlendImage({ image:imageA, mode:'add', alpha:0.5 }); //Add the second image fabric.Image.fromURL('my_image2.png',(imageB) => { imageB.scaleToHeight(100), imageB.scaleToWidth(100), canvas.add(imageB) }, {crossOrigin:''}); imageA.filters.push(filter); imageA.applyFilters(canvas.renderAll.bind(canvas)); },{crossOrigin:''})
Enter fullscreen mode Exit fullscreen modeOutput
Image Clipping
A very popular effect used today is image clipping, or rather image cropping, which is done to remove parts of an image that we don’t want. With Fabric.js, we can achieve this by using the
clipPath
method.First, we have to create our own
clipPath
using any Fabric.js object. Then, we’ll assign it to theclipPath
property of the object we want to clip. Here, we’ll use a circle object:
const clipPath = new fabric.Circle({ radius:150, top:200, left:250, // The originY and originX helps to center the image and the clipPath originY:"center", originX:"center" })
Enter fullscreen mode Exit fullscreen modeAfter that, we add it to the image that we want to clip using the filter method:
new fabric.Image.fromURL('my_image.png',(img) => { img.scaleToHeight(300) img.scaleToWidth(300) // positioning the image img.scale(o.5).set({top:50,left:100}) // Clip Path clipPath = new fabric.Circle({ radius:150, top:200, left:250, originY:"center", originX:"center" }) canvas.add(img) canvas.clipPath = clipPath; }); canvas.renderAll();
Enter fullscreen mode Exit fullscreen modeOutput
Image Blurring
Like other image filters, the blur filter accepts objects because they can be adjusted. A practical example is changing the brightness of an image. You can apply other image filters without any additional configurations (like grayscale, invert, or sepia). Other image filters like brightness, pixelate, tint, and noise all operate in this way, too. However, the difference is that their values are not the same. The brightness filter's actual brightness level ranges from-1 (full black) to 1 (full white). The noise filter's value is between 0 and 1000. For the blur filter, the value ranges from 0 to 10.
Syntax
new fabric.Image.filters.Blur(object)
Enter fullscreen mode Exit fullscreen modefabric.Image.fromURL('my_image.png', (image) => { image.scaleToHeight(300) image.ScaleToWidth(300) // Creating the blur filter const filter = new fabric.Image.filters.Blur({ blur:0.6 }) //push the filter instance to filters image.filter.push(filter) image.applyFilters() canvas.add(image) },{crossOrigin: 'anonymous'}); canvas.renderAll();
Enter fullscreen mode Exit fullscreen modeOutput
Conclusion
In conclusion, Fabric.js gives us an awesome experience when working with Canvas. It also provides a lot of features, some of which we covered in this article. For full details on some of the Fabric.js objects, you can review its documentation here.
What are some alternatives?
image - Encoding and decoding images in Rust
paper.js - The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey
InputBot - Rust library for creating global hotkeys, and simulating inputs
p5.js - p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
Kubewarden - Kubewarden is a policy engine for Kubernetes. It helps with keeping your Kubernetes clusters secure and compliant. Kubewarden policies can be written using regular programming languages or Domain Specific Languages (DSL) sugh as Rego. Policies are compiled into WebAssembly modules that are then distributed using traditional container registries.
three.js - JavaScript 3D Library.
Quicksilver - Quicksilver Project Source
Konva - Konva.js is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.
jpegoptim - jpegoptim - utility to optimize/compress JPEG files
d3 - Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
menyoki - Screen{shot,cast} and perform ImageOps on the command line 🌱 🏞️
GoJS, a JavaScript Library for HTML Diagrams - JavaScript diagramming library for interactive flowcharts, org charts, design tools, planning tools, visual languages.