mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +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
Notes:
sideshowbarker
2024-07-18 19:22:07 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/955eef83b01
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.
|
||||
*/
|
||||
|
||||
#include <AK/Base64.h>
|
||||
#include <AK/Checked.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/PNGWriter.h>
|
||||
#include <LibWeb/CSS/StyleResolver.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
|
||||
|
@ -102,4 +104,14 @@ bool HTMLCanvasElement::create_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 height() const;
|
||||
|
||||
String to_data_url(const String& type, Optional<double> quality) const;
|
||||
|
||||
private:
|
||||
virtual RefPtr<Layout::Node> create_layout_node() override;
|
||||
|
||||
|
|
|
@ -4,4 +4,6 @@ interface HTMLCanvasElement : HTMLElement {
|
|||
readonly attribute unsigned long width;
|
||||
readonly attribute unsigned long height;
|
||||
|
||||
USVString toDataURL(optional DOMString type = "image/png", optional double quality);
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue