🎨 Jetpack Compose canvas library that helps you draw paths, images on canvas with color pickers and palettes

Overview



License API Build Status Android Weekly Dokka


🎨 Jetpack Compose canvas library that helps you draw paths and images on canvas with color pickers and palettes. Sketchbook also provides useful components and functions that can easily interact with canvas.


Preview

Contribution πŸ’™

Sketchbook is maintained by Stream. If you’re interested in adding powerful In-App Messaging to your app, check out the Stream Chat Tutorial for Jetpack Compose! Also, anyone can contribute to improving code, docs, or something following our Contributing Guideline.

Download

Maven Central

Gradle

Add the dependency below to your module's build.gradle file:

dependencies {
    implementation "io.getstream:sketchbook:1.0.2"
}

SNAPSHOT

See how to import the snapshot

Including the SNAPSHOT

Snapshots of the current development version of Sketchbook are available, which track the latest versions.

To import snapshot versions on your project, add the code snippet below on your gradle file.

repositories {
   maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

Next, add the below dependency to your module's build.gradle file.

dependencies {
    implementation "io.getstream:sketchbook:1.0.2-SNAPSHOT"
}

Usage

First, you should initialize SketchbookController, which allows you to control the Sketchbook and all components.

private val sketchbookController = rememberSketchbookController()

Next, you can implement your drawing canvas with the Sketchbook composable function:

Sketchbook(
    modifier = Modifier.fillMaxSize(),
    controller = sketchbookController,
    backgroundColor = Color.White
)

Sketchbook

Sketchbook implements canvas, which allows you to draw paths with custom properties. It interacts with SketchbookController to control other components. You can use the Sketchbook as the following example:

Sketchbook(
    modifier = Modifier.fillMaxSize(),
    controller = sketchbookController,
    backgroundColor = Color.White,
    onEventListener = { x, y -> .. },
    onRevisedListener = { canUndo, canRedo -> .. }
)

SketchbookController

SketchbookController interacts with Sketchbook and it allows you to control the canvas and all of the components with it.

Undo and Redo

SketchbookController supports undo and redo to cancel or reverse drawn paths and recover the previous canvas state. You can implement it with the following functions below:

sketchbookController.undo() // undo the drawn path if possible.
sketchbookController.redo() // redo the drawn path if possible.

Erase Mode

By enabling Erase Mode, you can erase the colored paths following the transparent path. You can set the mode with the function below:

sketchbookController.setEraseMode(true)

Also, you can toggle the erase mode with the following function below:

sketchbookController.toggleEraseMode()

You can get the State of the erase mode with the following function below:

val isEraseMode = sketchbookController.isEraseMode.value

Note: If you use the erase mode, make sure you set the backgroundColor on the Sketchbook properly.

You can changes the radius size of the erase circle:

sketchbookController.setEraseRadius(50f)

Customize Paint

You can custom the paint to support various drawing options as the following:

.setPaintColor(color) 
.setPaintAlpha(alpha) 
.setPaintStrokeWidth(stroke)
.setPaintShader(shader)
.setLinearShader(colorList)
.setPaintingStyle(paintStyle)
.setPathEffect(pathEffect)

Also, you can set your own paint with the setPaint method:

sketchbookController.setPaint(paint)

You can set the rainbow shader to the paint with the setRainbowShader method:

sketchbookController.setRainbowShader()

ImageBitmap

Sketchbook supports to set an ImageBitmp on the canvas and draw paths over the bitmap. You can set an initial ImageBitmap on the Sketchbook composable as the following:

Sketchbook(
    imageBitmap = ImageBitmap.imageResource(R.drawable.poster),
    ..
)

Also, you can set an ImageBitmap dynamically with the SketchbookController as the following:

sketchbookController.setImageBitmap(imageBitmap)

You can clear the image bitmap on canvas with the clearImageBitmap method:

sketchbookController.clearImageBitmap()

Note: This demo project demonstrates an image picker with ModernStorage's Photo Picker.

Sketchbook Bitmap

You can get the final ImageBitmap of the current canvas of the Sketchbook with the following:

val imageBitmap = sketchbookController.getSketchbookBitmap()

If you'd like to get the Android's bitmap, you can get it as the following:

val bitmap = sketchbookController.getSketchbookBitmap().asAndroidBitmap()

Clear

You can clear all the drawn paths and paths and the image bitmap as the following:

sketchbookController.clear()

Also, you can clear only the drawn paths and redo paths as the following:

sketchbookController.clearPaths()

PaintColorPalette

PaintColorPalette provides a color palette to let users choose desired colors from a provided color list. It provides default color palettes and shapes, which are fully customizable. You can simply implement this as the following example:

PaintColorPalette(
    controller = sketchbookController,
    borderColor = MaterialTheme.colors.onPrimary
)

You can customize UI themes with PaintColorPaletteTheme as the following example:

PaintColorPalette(
    theme = PaintColorPaletteTheme(
        shape = CircleShape,
        itemSize = 48.dp,
        selectedItemSize = 58.dp,
        borderColor = Color.White,
        borderWidth = 2.dp,
        borderColor = MaterialTheme.colors.onPrimary
    ),

Also, you can set an index for selecting a color initially with the initialSelectedIndex parameter:

initialSelectedIndex = 2

onColorSelected

You can track the selected color's index and color value with the onColorSelected listener:

PaintColorPalette(
    onColorSelected = { index, color -> .. },
)

Header and Footer

You can add your own composable on the very first of the palette or on the end as the following example:

PaintColorPalette(
    header = { Text("Header") },
    footer = { Text("Footer") }
)

Custom Content

You can customize the entire content with your own composable as the following example:

PaintColorPalette(
    content = { index, color ->
        Box(modifier = Modifier
            .size(60.dp)
            .background(color)
            .clickable { .. }
        )
    }
)

Note: Make sure if you use the custom content, you should implement all interactions and listeners by yourself.

ColorPickerDialog

Sketchbook supports ColorPickerDialog, which lets you choose your desired color from an HSV color palette. You can implement ColorPickerDialog with the following codes:

val expandColorPickerDialog = remember { mutableStateOf(false) }
Image(
    modifier = 
        Modifier.clickable { expandColorPickerDialog.value = true },
    bitmap = ImageBitmap.imageResource(R.drawable.palette),
    contentDescription = null
)

ColorPickerDialog(
    controller = sketchbookController,
    expanded = expandColorPickerDialog,
    initialColor = controller.currentPaintColor.value,
    onColorSelected = { color ->
        controller.setPaintShader(null)
        controller.setSelectedColorIndex(-1)
    }
)

ColorPickerPaletteIcon

ColorPickerPaletteIcon implements the example code above internally, so you can use like an icon, which shows up a color picker dialog as the following example:

ColorPickerPaletteIcon(
     modifier = Modifier
        .size(60.dp)
        .padding(6.dp),
    controller = sketchbookController,
    bitmap = ImageBitmap.imageResource(R.drawable.palette)
)

You can use the ColorPickerPaletteIcon with hearder or footer custom composable for the PaintColorPalette.

PaintColorPalette(
    header = {
        ColorPickerPaletteIcon(
            ..
        )
    }
)

Find this library useful? ❀️

Support it by joining stargazers for this repository. ⭐
Also, follow Stream on Twitter for our next creations!

License

Copyright 2022 Stream.IO, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
🌈 Palette - A color picker library made in Jetpack Compose
🌈 Palette - A color picker library made in Jetpack Compose

🌈 Palette A color picker library made in Jetpack Compose. Including in your project Gradle Add it in your root build.gradle at the end of repositorie

πŸ‚ Jetpack Compose image loading library which can fetch and display network images using Glide, Coil, and Fresco.
πŸ‚ Jetpack Compose image loading library which can fetch and display network images using Glide, Coil, and Fresco.

Landscapist πŸ‚ Jetpack Compose image loading library which can fetch and display network images using Glide, Coil, Fresco Usecase You can see the use

Dynamic Badge with customizable features as max number before displaying with +, color, shadow, border, corner radius, font properties and more written with Jetpack Compose
Dynamic Badge with customizable features as max number before displaying with +, color, shadow, border, corner radius, font properties and more written with Jetpack Compose

βœοΈπŸ“Œ Dynamic Badge with customizable features as max number before displaying with +, color, shadow, border, corner radius, font properties and more written with Jetpack Compose. Displays numbers either in circle or rounded rectangle shape based on badge count and selected threshold to transform from circle to rounded rectangle.

Row Coloumn Box Compose Constraint Layout Modifier.xyz Animator Tween animation MutableState Creating custom composable Corners Canvas LaunchedEffect
Row Coloumn Box Compose Constraint Layout Modifier.xyz Animator Tween animation MutableState Creating custom composable Corners Canvas LaunchedEffect

Row Coloumn Box Compose Constraint Layout Modifier.xyz Animator Tween animation MutableState Creating custom composable Corners Canvas LaunchedEffect

πŸš€πŸžπŸ’ͺ Collection of Images, Modifiers, utility functions for Jetpack Compose to expand and enrich displaying, manipulating, scaling, resizing, zooming, and getting cropped ImageBitmap based on selection area

Collection of Images, Modifiers, utility functions for Jetpack Compose to expand and enrich displaying, manipulating, scaling, resizing, zooming, and getting cropped ImageBitmap based on selection area, before/after image to with handle to show partial of both images and more is cooking up

Simple example how you can use dynamic color image vector in your app.
Simple example how you can use dynamic color image vector in your app.

Dynamic Color ImageVector Simple example how you can use dynamic color image vector in your app. How to use 1. Create a xml image vector The content o

Butterfly - Butterfly helps to build adaptive and responsive UIs for Android with Jetpack WindowManager
Butterfly - Butterfly helps to build adaptive and responsive UIs for Android with Jetpack WindowManager

πŸ¦‹ Butterfly helps to build adaptive and responsive UIs for Android with Jetpack

A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

πŸš€πŸ“’πŸ“ Indicators for Horizontal or Vertical Pager with different orientation, color, size options and optional touch feature.

Compose Pager Indicator Indicators for Horizontal or Vertical pager with different orientation, color, size options and optional touch feature. indica

Comments
  • Sketchbook not working in ModalBottomSheetLayout

    Sketchbook not working in ModalBottomSheetLayout

    I use sketchbook version 1.0.4. If I add a sketchbook to ModalBottomSheetLayout only drawing that work are horizontal lines. ModalBottomSheetLayout steel events and sketchbook don't draw anything if I draw vertically.

    opened by drdla49 2
  • alpha is not accurate work

    alpha is not accurate work

    If you set the alpha value to , and draw a line, a gradient is gradually created, and the first part becomes darker. Paths drawn previously during drawPath() overlap, so the alpha value does not seem to work properly Screenshot_20220602-172218 Screenshot_20220602-172253 .

    opened by RoyLee-Opsv 0
Releases(1.0.4)
  • 1.0.4(Jun 29, 2022)

    What's Changed

    • Bump Compose version to 1.2.0-rc03 and other dependencies by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/7
    • Improve library performance with Baseline Profiles by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/8
    • Update Compose compiler to 1.2.0 by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/10
    • Prepare release 1.0.4 by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/9

    Full Changelog: https://github.com/GetStream/sketchbook-compose/compare/1.0.3...1.0.4

    Source code(tar.gz)
    Source code(zip)
  • 1.0.3(Mar 25, 2022)

    What's Changed

    • Pass MotionEvent value to the onEventListener by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/5
    • Split initialization of the Sketchbook composable into the SideEffect and DisposableEffect by @skydoves

    Full Changelog: https://github.com/GetStream/sketchbook-compose/compare/1.0.2...1.0.3

    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Mar 10, 2022)

    What's Changed

    • Implement addDrawPath() methods to SketchbookController by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/3
    • Implement onPathListener for Sketchbook Composable by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/4
    • Bump AGP to 7.1.2 and Gradle to 7.4 by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/2

    Full Changelog: https://github.com/GetStream/sketchbook-compose/compare/1.0.1...1.0.2

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Feb 28, 2022)

    πŸŽ‰ New version 1.0.1 has been released. πŸŽ‰

    What's Changed

    • Bump Jetpack Compose to 1.1.1 by @skydoves in https://github.com/GetStream/sketchbook-compose/pull/1
    • Refactored internal functions to stateless.

    New Contributors

    • @skydoves made their first contribution in https://github.com/GetStream/sketchbook-compose/pull/1

    Full Changelog: https://github.com/GetStream/sketchbook-compose/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Feb 23, 2022)

    πŸŽ‰ Sketchbook 1.0.0 has been released! πŸŽ‰

    🎨 Jetpack Compose canvas library that helps you to draw paths, images on canvas with color pickers and palettes.

    Source code(tar.gz)
    Source code(zip)
Owner
Stream
Build scalable newsfeeds, activity streams, chat and messaging in a few hours instead of weeks
Stream
An exploratory playground library to figure out how to Draw and animate using Jetpack Compose

Jetpack Compose Chart Library This is an exploratory playground library to figure out how to Draw and animate using Android Jetpack Compose library. C

null 2 Sep 8, 2022
πŸ—Ί Android Developer Roadmap 2022 suggests learning paths to understanding Android development.

Android Developer Roadmap 2022 English | ν•œκ΅­μ–΄ Android Developer Roadmap 2022 suggests learning paths to understanding Android development. You can read

Jaewoong Eum 5.2k Jan 8, 2023
πŸ”¦ Showkase is an annotation-processor based Android library that helps you organize, discover, search and visualize Jetpack Compose UI elements

Showkase is an annotation-processor based Android library that helps you organize, discover, search and visualize Jetpack Compose UI elements. With minimal configuration it generates a UI browser that helps you easily find your components, colors & typography. It also renders your components in common situations like dark mode, right-to-left layouts, and scaled fonts which help in finding issues early.

Airbnb 1.7k Jan 2, 2023
Gmail clone project, that uses Jetpack Compose to draw UI content for gmail home screen

Gmail clone project, that uses Jetpack Compose to draw UI content for gmail home screen following Udemy course: Android 12 Jetpack Compose Developer Course - From 0 To Hero

SaraAlshamy 3 Sep 2, 2022
This repo is to demonstrate the jetpack compose 's canvas api's usage and creating an icon pack using the basic functions.

Jetpack Compose Canvas API Demo App We all know that Jetpack Compose has now reached the 1.0.0 release milestone. This is a huge change we can say for

vikas kumar 32 Dec 15, 2022
Exercising Compose for Desktop and Compose for Web (Canvas)

Compose Counting Grid A simple application to check Compose for Desktop and Compose for Web (Canvas) drawing speeds when drawing grids (or tables) wit

null 6 Nov 11, 2022
FullMangement - an application that helps you manage your tasks effectively. built with the latest tachs like Compose UI, Jetpack libraries, and MVVM design pattern.

Full Management is an application that helps you manage your tasks effectively. built with the latest tachs like Compose UI, Jetpack libraries and MVVM design pattern.

Amr algnyat 4 Nov 1, 2022
Welcome Fruit Ninja πŸ₯ on Jetpack Compose Desktop πŸš€, using Canvas API 🎨

Compose-Fruit-Ninja ?? Welcome Fruit Ninja on Jetpack Compose Desktop ?? , using Canvas API ?? Featured on jetc-dev How to Run From gradle tab from ri

Chetan Gupta 54 Nov 2, 2022
Alien invasion πŸ‘Ύ gane is back! this time specially on Jetpack Compose Desktop πŸš€, using Canvas API 🎨

Compose Space-Invaders ?? Alien invasion ?? is back! this time specially on Jetpack Compose Desktop ?? , using Canvas API ?? Featured on Compose Diges

Chetan Gupta 58 Aug 6, 2022
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Md. Mahmudul Hasan Shohag 186 Jan 1, 2023