mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:47:45 +00:00
LibWeb: Add basic support for HTMLCanvasElement.toDataURL() :^)
This allows you to serialize a <canvas> element's bitmap into a data: URI. Pretty neat! :^)
This commit is contained in:
parent
6793574003
commit
955eef83b0
3 changed files with 16 additions and 0 deletions
|
@ -24,8 +24,10 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Base64.h>
|
||||||
#include <AK/Checked.h>
|
#include <AK/Checked.h>
|
||||||
#include <LibGfx/Bitmap.h>
|
#include <LibGfx/Bitmap.h>
|
||||||
|
#include <LibGfx/PNGWriter.h>
|
||||||
#include <LibWeb/CSS/StyleResolver.h>
|
#include <LibWeb/CSS/StyleResolver.h>
|
||||||
#include <LibWeb/DOM/Document.h>
|
#include <LibWeb/DOM/Document.h>
|
||||||
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
|
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
|
||||||
|
@ -102,4 +104,14 @@ bool HTMLCanvasElement::create_bitmap()
|
||||||
return m_bitmap;
|
return m_bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String HTMLCanvasElement::to_data_url(const String& type, [[maybe_unused]] Optional<double> quality) const
|
||||||
|
{
|
||||||
|
if (!m_bitmap)
|
||||||
|
return {};
|
||||||
|
if (type != "image/png")
|
||||||
|
return {};
|
||||||
|
auto encoded_bitmap = Gfx::PNGWriter::encode(*m_bitmap);
|
||||||
|
return URL::create_with_data(type, encode_base64(encoded_bitmap), true).to_string();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,8 @@ public:
|
||||||
unsigned width() const;
|
unsigned width() const;
|
||||||
unsigned height() const;
|
unsigned height() const;
|
||||||
|
|
||||||
|
String to_data_url(const String& type, Optional<double> quality) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual RefPtr<Layout::Node> create_layout_node() override;
|
virtual RefPtr<Layout::Node> create_layout_node() override;
|
||||||
|
|
||||||
|
|
|
@ -4,4 +4,6 @@ interface HTMLCanvasElement : HTMLElement {
|
||||||
readonly attribute unsigned long width;
|
readonly attribute unsigned long width;
|
||||||
readonly attribute unsigned long height;
|
readonly attribute unsigned long height;
|
||||||
|
|
||||||
|
USVString toDataURL(optional DOMString type = "image/png", optional double quality);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue