` with
305 |
306 | ```js
307 |
308 | (...)
309 |
310 | function getPacks(token) {
311 | return fetchFromAPI(`${HOST}/direct/packs`, token);
312 | }
313 |
314 | (... in loadBitmojiContent)
315 | const packs = await getPacks(token);
316 | const tagsContainer = document.getElementById('tags');
317 | packs.forEach(pack => {
318 | const packLink = document.createElement('a');
319 | packLink.innerHTML = pack.name;
320 | packLink.style.marginRight = '24px';
321 | packLink.href = "#";
322 | tagsContainer.appendChild(packLink);
323 | });
324 | ```
325 |
326 | ### Showing stickers within pack when clicking
327 |
328 | Now you can add the event handler to show the relevant content when clicking one of the tags. To do so, the `id` field within the pack is used. `popular` is just another pack `id` field. Let's refactor `getPopularSticker` to accept any packId
329 |
330 | ```js
331 | function getPackStickers(token, packId) {
332 | return fetchFromAPI(`${HOST}/direct/pack/${packId}`, token);
333 | }
334 |
335 | (... in loadBitmojiContent)
336 | const stickers = await getPackStickers(token, 'popular');
337 | ```
338 |
339 | Finally, let's add the click handler for the tags
340 |
341 | ```js
342 | packLink.addEventListener('click', async (e) => {
343 | e.preventDefault();
344 | const stickers = await getPackStickers(token, pack.id);
345 | renderStickers(stickers);
346 | });
347 | ```
348 |
349 | ## Picking a sticker
350 |
351 | When users select a sticker an action like should happen. For instance, you may want to share the sticker in a 1:1 conversation in a chat application. To do this the same URI used to display the sticker preview may be used.
352 |
353 | Let's update the app UI to include an area to display the selected image
354 | ```html
355 |
356 |
364 |
365 |
![]()
366 |
367 |
368 |
369 | ```
370 |
371 | Now in the function where we render the stickers, we can add a handler to call a function with the click event is triggered:
372 | ```js
373 | function renderStickers(stickers) {
374 | const stickerPicker = document.getElementById('stickers');
375 | stickerPicker.innerHTML = '';
376 | stickers.forEach(sticker => {
377 | const image = new Image(100, 100);
378 | image.src = sticker.uri;
379 | image.onclick = () => shareSticker(sticker);
380 | stickerPicker.appendChild(image);
381 | });
382 | }
383 |
384 | function shareSticker(sticker) {
385 | document.getElementById('share-area').src = sticker.uri;
386 | }
387 | ```
388 |
389 | If you test this code it will work, but the shared image will apear pixelated. To allow faster loading times in your picker, stickers are loaded on their `thumbnail` size. This may be undesirable for the sharing use case where a higher image resolution may be required. It is trivial to do this. Stickers have a number of query parameters attached to them, you can manipulate the `size` parameter to load a bigger size image. The available options are:
390 | - `default`: 400x400 pixels
391 | - `large`: 800x800 pixels
392 |
393 | Adding the size parameter to the example:
394 | ```js
395 | function shareSticker(sticker) {
396 | document.getElementById('share-area').src = sticker.uri.replace('size=thumbnail', 'size=default');
397 | }
398 | ```
399 |
400 | Bitmoji may be notified that a sticker has been picked to share. A stub example could be:
401 |
402 | ```js
403 | function shareSticker(sticker) {
404 | document.getElementById('share-area').src = sticker.uri.replace('size=thumbnail', 'size=default');
405 | fetch(`${HOST}/direct/event/${sticker.on_share}`, { method: 'post' });
406 | }
407 | ```
408 |
409 | ## Customizing content
410 |
411 | ### Locale
412 | The `/packs`, `/pack/{packId}` and `/search` endpoints support a `locale` query parameter. The content will be returned in the locale of your choice if supported. I will default to English.
413 |
414 | The locales supported currently are: `en`, `ar`, `de`, `es`, `fr`, `it`, `ja`, `ko` and `pt`.
415 |
416 | If you don't provide an explicit `locale` parameter but the `accept-language` HTTP header is set, the API will resolve based on the header.
417 |
418 | ### Time
419 | The `/packs` and `/pack/{packId}` endpoints support a `time` parameter.
420 |
421 | Bitmoji content is variable depending on time of day. To be able to display the right content to your users you can send a date string in the `time` query parameter and timely content will be returned if available.
422 |
423 | Example of valid time strings are `2020-10-15T13:05:05`(no timezone) or `2020-10-15T18:02:04-04:00`(with timezone).
424 |
425 | ### Pagination
426 |
427 | The `/pack/{packId}` and `/search` endpoints support optional `limit` and `page` query parameters to paginate through the returned data. `limit`
428 | should be a positive integer while `page` should be 0 or positive. If only a `limit` param is passed, the `page` defaults to 0. Specifying only
429 | `page` doesn't affect the results.
430 |
431 | To help with pagination, the result from Bitmoji direct API also returns metadata in the JSON result, this can be ignored if you aren't using
432 | pagination. This includes the field `next` - URL path with query pointing to the next page, `prev` URL path with query pointing to the previous
433 | page and `total` - the total number of elements for this query (this can be used to set the page size). `next` and `prev` are null if
434 | pagination params weren't specified or if no `next` or `prev` page exists. Note that `next` and `prev` preserve original query params.
435 |
436 | As an example, the result for `https://bitmoji.api.snapchat.com/direct/search?q=hi&locale=en&limit=10&page=2` is as follows:
437 |
438 | ```json
439 | {
440 | "data": [...],
441 | "metadata": {
442 | "next": "/direct/search?q=hi&locale=en&limit=10&page=3",
443 | "prev": "/direct/search?q=hi&locale=en&limit=10&page=1",
444 | "total": 56
445 | }
446 | }
447 | ```
448 |
449 | ## Full code for reference
450 |
451 | ```html
452 |
453 |
454 |
455 |
456 |
457 |
Bitmoji Picker
458 |
459 |
460 |
461 |
462 |
470 |
471 |
![]()
472 |
473 |
474 |
475 |
511 |
512 |
584 |
585 |
586 |
587 |
588 | ```
589 |
--------------------------------------------------------------------------------
/docs/vanilla-sticker-picker/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Bitmoji Picker
7 |
8 |
9 |
10 |
11 |
19 |
20 |
![]()
21 |
22 |
23 |
24 |
60 |
61 |
133 |
134 |
135 |
136 |
--------------------------------------------------------------------------------
/images/bfd_logo_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bitmoji/BitmojiForDevelopers/3d0d0d166b9f5e8f64bc1ac9fee683d974456b31/images/bfd_logo_black.png
--------------------------------------------------------------------------------
/images/bfd_logo_green.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bitmoji/BitmojiForDevelopers/3d0d0d166b9f5e8f64bc1ac9fee683d974456b31/images/bfd_logo_green.png
--------------------------------------------------------------------------------
/images/bfd_logo_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bitmoji/BitmojiForDevelopers/3d0d0d166b9f5e8f64bc1ac9fee683d974456b31/images/bfd_logo_white.png
--------------------------------------------------------------------------------