mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:07:45 +00:00
LibWeb: Add ImageData objects and implement 2D context putImageData()
An ImageData is a wrapper around a Bitmap wrapper around a JS::Uint8ClampedArray.
This commit is contained in:
parent
54133c683d
commit
2d4c91df8e
10 changed files with 359 additions and 0 deletions
|
@ -33,8 +33,10 @@
|
|||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
|
||||
#include <LibWeb/Bindings/HTMLImageElementWrapper.h>
|
||||
#include <LibWeb/Bindings/ImageDataWrapper.h>
|
||||
#include <LibWeb/DOM/CanvasRenderingContext2D.h>
|
||||
#include <LibWeb/DOM/HTMLImageElement.h>
|
||||
#include <LibWeb/DOM/ImageData.h>
|
||||
|
||||
namespace Web {
|
||||
namespace Bindings {
|
||||
|
@ -62,6 +64,9 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering
|
|||
put_native_function("moveTo", move_to, 0);
|
||||
put_native_function("lineTo", line_to, 0);
|
||||
|
||||
put_native_function("createImageData", create_image_data, 2);
|
||||
put_native_function("putImageData", put_image_data, 3);
|
||||
|
||||
put_native_property("lineWidth", line_width_getter, line_width_setter);
|
||||
}
|
||||
|
||||
|
@ -235,5 +240,37 @@ JS::Value CanvasRenderingContext2DWrapper::line_to(JS::Interpreter& interpreter)
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS::Value CanvasRenderingContext2DWrapper::create_image_data(JS::Interpreter& interpreter)
|
||||
{
|
||||
auto* impl = impl_from(interpreter);
|
||||
if (!impl)
|
||||
return {};
|
||||
i32 width = interpreter.argument(0).to_i32();
|
||||
i32 height = interpreter.argument(1).to_i32();
|
||||
auto image_data = impl->create_image_data(interpreter.global_object(), width, height);
|
||||
return wrap(interpreter.heap(), *image_data);
|
||||
}
|
||||
|
||||
JS::Value CanvasRenderingContext2DWrapper::put_image_data(JS::Interpreter& interpreter)
|
||||
{
|
||||
auto* impl = impl_from(interpreter);
|
||||
if (!impl)
|
||||
return {};
|
||||
|
||||
auto* image_data_object = interpreter.argument(0).to_object(interpreter.heap());
|
||||
if (!image_data_object)
|
||||
return {};
|
||||
|
||||
if (StringView(image_data_object->class_name()) != "ImageDataWrapper") {
|
||||
return interpreter.throw_exception<JS::TypeError>("putImageData called with non-ImageData");
|
||||
}
|
||||
|
||||
auto& image_data = static_cast<ImageDataWrapper*>(image_data_object)->impl();
|
||||
auto x = interpreter.argument(1).to_double();
|
||||
auto y = interpreter.argument(2).to_double();
|
||||
impl->put_image_data(image_data, x, y);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue