mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-27 03:37:53 +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:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user