JSHTMLCanvasElementCustom.cpp [plain text]
#include "config.h"
#include "JSHTMLCanvasElement.h"
#include "HTMLCanvasElement.h"
#include "JSCanvasRenderingContext2D.h"
#include <bindings/ScriptObject.h>
#include <wtf/GetPtr.h>
#if ENABLE(WEBGL)
#include "JSWebGLContextAttributes.h"
#include "JSWebGLRenderingContextBase.h"
#endif
using namespace JSC;
namespace WebCore {
JSValue JSHTMLCanvasElement::getContext(ExecState& state)
{
auto& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (UNLIKELY(state.argumentCount() < 1))
return throwException(&state, scope, createNotEnoughArgumentsError(&state));
auto contextId = convert<IDLDOMString>(state, state.uncheckedArgument(0), StringConversionConfiguration::Normal);
RETURN_IF_EXCEPTION(scope, JSValue());
if (HTMLCanvasElement::is2dType(contextId))
return toJS<IDLNullable<IDLInterface<CanvasRenderingContext2D>>>(state, *globalObject(), static_cast<CanvasRenderingContext2D*>(wrapped().getContext2d(contextId)));
#if ENABLE(WEBGL)
if (HTMLCanvasElement::is3dType(contextId)) {
auto attributes = convert<IDLDictionary<WebGLContextAttributes>>(state, state.argument(1));
RETURN_IF_EXCEPTION(scope, JSValue());
return toJS<IDLNullable<IDLInterface<WebGLRenderingContextBase>>>(state, *globalObject(), static_cast<WebGLRenderingContextBase*>(wrapped().getContextWebGL(contextId, WTFMove(attributes))));
}
#endif
return jsNull();
}
JSValue JSHTMLCanvasElement::toDataURL(ExecState& state)
{
VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto type = convert<IDLNullable<IDLDOMString>>(state, state.argument(0));
RETURN_IF_EXCEPTION(scope, JSC::JSValue());
std::optional<double> quality;
auto qualityValue = state.argument(1);
if (qualityValue.isNumber())
quality = qualityValue.toNumber(&state);
auto result = wrapped().toDataURL(type, quality);
if (result.hasException()) {
propagateException(state, scope, result.releaseException());
return { };
}
return jsString(&state, result.releaseReturnValue());
}
}