Slightly clean up the final URL creation in canvas.toDataURL

This commit is contained in:
Anthony Ramine 2018-10-03 10:43:41 +02:00
parent a3392610c3
commit cfd446218b

View file

@ -396,19 +396,16 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
}, },
}; };
// Only handle image/png for now. // FIXME: Only handle image/png for now.
let mime_type = "image/png"; let mut png = Vec::new();
PNGEncoder::new(&mut png)
let mut encoded = Vec::new(); .encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8))
{ .unwrap();
let encoder: PNGEncoder<&mut Vec<u8>> = PNGEncoder::new(&mut encoded); let mut url = "data:image/png;base64,".to_owned();
encoder // FIXME(nox): Should this use base64::URL_SAFE?
.encode(&raw_data, self.Width(), self.Height(), ColorType::RGBA(8)) // FIXME(nox): https://github.com/alicemaz/rust-base64/pull/56
.unwrap(); base64::encode_config_buf(&png, base64::STANDARD, &mut url);
} Ok(USVString(url))
let encoded = base64::encode(&encoded);
Ok(USVString(format!("data:{};base64,{}", mime_type, encoded)))
} }
} }