JavaScript Canvas - Fetch API |
Written by Ian Elliot | ||||
Tuesday, 16 July 2024 | ||||
Page 3 of 3
Request ObjectThings are only a little more complicated when you want to do something more than just a get. You can specify a second parameter, the init object, in the call to control the type of request made. The init object has lots of properties, but the most important are:
If you want to repeatedly fetch the same resource it is better to create a Request object which has all of the properties of the init object plus a URL property. You can pass the Request object to fetch in place of the URL parameter and it acts as the init object as well. So the previous fetch could be implemented as: async function getText(){ var request=new Request(’myFile.txt’, { method:’GET’ Notice that you can reuse a Request object even if you have streamed the body data of its associated Response. However, as already commented, you cannot reuse a Response object after you have read its body data. You can also obtain a duplicate Request or Response object using the clone method. This can be useful if you aren’t sure that the response will be valid. For example, to first check to see if the response is valid json we could use: var response = await fetch(request); var res2 = response.clone(); try{ console.log(await res2.json()); } catch (e) { console.log(await response.text()); } If the response isn’t valid json it is displayed as text. Notice you have to clone the response before trying to retrieve the body. You cannot clone a stream that has been read. You can use a fluent style to make this look neater: var response = await fetch(request); try{ console.log(await response.clone().json()); } catch (e) { console.log(await response.text()); } The clone method introduces a buffer into the equation and the data is streamed into a local buffer from where it can be accessed a second time. This has the side effect of keeping the data in memory until all of the copies are read or disposed of. Downloading Graphics from the ServerUsing fetch this is very easy. All you need is a call to fetch with the correct URL and then use one of the formatted reads to get the data. The problem is that there is no format provided that works simply with graphics data. You can read the file as a blob but then you have to create an object URL, read the data into an Image object and draw to the canvas. For example: async function fetchGraphic(url){ var response=await fetch(url); var blob=await response.blob(); return new Promise( function (resolve, reject) { var url = URL.createObjectURL(blob); var img = new Image(); img.addEventListener("load", The only advantage of this overloading of the URL directly into the Image object is that the load occurs with the fetch rather than on setting the src attribute. In fact the more direct: async function fetchGraphic2(url) { return new Promise( function (resolve, reject) { var img = new Image(); img.addEventListener("load", is also slightly faster because we don’t have to load the file and then load the blob into the Image object. Notice that this doesn’t mean that fetch is useless as, while it isn’t the best way to load graphics, it is completely general and it also works with upload. Sometimes we need to load the file as an array to do some work on it before using it as a graphic, see the PCX example at the end of the next chapter. In Chapter but not in this extract
Summary
Now available as a paperback or ebook from Amazon.JavaScript Bitmap Graphics
|
||||
Last Updated ( Wednesday, 17 July 2024 ) |