OLD | NEW |
1 SkPaint | 1 SkPaint |
2 ======= | 2 ======= |
3 | 3 |
4 *color, stroke, font, effects* | 4 *color, stroke, font, effects* |
5 | 5 |
6 - [SkXfermode](#SkXfermode) - transfer modes | 6 - [SkXfermode](#SkXfermode) - transfer modes |
7 - [ShShader](#ShShader) - gradients and patterns | 7 - [ShShader](#ShShader) - gradients and patterns |
8 - [SkMaskFilter](#SkMaskFilter) - modifications to the alpha mask | 8 - [SkMaskFilter](#SkMaskFilter) - modifications to the alpha mask |
9 - [SkColorFilter](#SkColorFilter) - modify the source color before applying th
e | 9 - [SkColorFilter](#SkColorFilter) - modify the source color before applying th
e |
| 10 - [SkPathEffect](#SkPathEffect) - modify to the geometry before it |
| 11 generates an alpha mask. |
10 | 12 |
11 Anytime you draw something in Skia, and want to specify what color it | 13 Anytime you draw something in Skia, and want to specify what color it |
12 is, or how it blends with the background, or what style or font to | 14 is, or how it blends with the background, or what style or font to |
13 draw it in, you specify those attributes in a paint. | 15 draw it in, you specify those attributes in a paint. |
14 | 16 |
15 Unlike `SkCanvas`, paints do not maintain an internal stack of state | 17 Unlike `SkCanvas`, paints do not maintain an internal stack of state |
16 (i.e. there is no save/restore on a paint). However, paints are | 18 (i.e. there is no save/restore on a paint). However, paints are |
17 relatively light-weight, so the client may create and maintain any | 19 relatively light-weight, so the client may create and maintain any |
18 number of paint objects, each set up for a particular use. Factoring | 20 number of paint objects, each set up for a particular use. Factoring |
19 all of these color and stylistic attribute out of the canvas state, | 21 all of these color and stylistic attribute out of the canvas state, |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 ct[i] = x < 0 ? 0 : x > 255 ? 255 : x; | 508 ct[i] = x < 0 ? 0 : x > 255 ? 255 : x; |
507 } | 509 } |
508 SkColorFilter* cf = SkTableColorFilter::CreateARGB(NULL, ct, ct, ct); | 510 SkColorFilter* cf = SkTableColorFilter::CreateARGB(NULL, ct, ct, ct); |
509 SkPaint paint; | 511 SkPaint paint; |
510 paint.setColorFilter(cf); | 512 paint.setColorFilter(cf); |
511 cf->unref(); | 513 cf->unref(); |
512 canvas->drawBitmap(source, 0, 0, &paint); | 514 canvas->drawBitmap(source, 0, 0, &paint); |
513 | 515 |
514 <a href="https://fiddle.skia.org/c/0d3d339543afa1b10c60f9826f264c3f"> | 516 <a href="https://fiddle.skia.org/c/0d3d339543afa1b10c60f9826f264c3f"> |
515 <img src="https://fiddle.skia.org/i/0d3d339543afa1b10c60f9826f264c3f_raster.
png"></a> | 517 <img src="https://fiddle.skia.org/i/0d3d339543afa1b10c60f9826f264c3f_raster.
png"></a> |
| 518 |
| 519 |
| 520 <span id="SkPathEffect"></span> |
| 521 |
| 522 SkPathEffect |
| 523 ------------ |
| 524 |
| 525 * SkPath2DPathEffect: Stamp the specified path to fill the shape, |
| 526 using the matrix to define the latice. |
| 527 |
| 528 <!--?prettify lang=cc?--> |
| 529 |
| 530 void draw(SkCanvas* canvas) { |
| 531 SkScalar scale = 10.0f; |
| 532 SkPath path; |
| 533 static const int8_t pts[] = { 2, 2, 1, 3, 0, 3, 2, 1, 3, 1, |
| 534 4, 0, 4, 1, 5, 1, 4, 2, 4, 3, 2, 5, 2, 4, 3, 3, 2, 3 }; |
| 535 path.moveTo(2 * scale, 3 * scale); |
| 536 for (size_t i = 0 ; i < sizeof(pts)/sizeof(pts[0]); i += 2) { |
| 537 path.lineTo(pts[i] * scale, pts[i + 1] * scale); |
| 538 } |
| 539 path.close(); |
| 540 SkMatrix matrix = SkMatrix::MakeScale(4 * scale); |
| 541 SkAutoTUnref<SkPathEffect> pathEffect( |
| 542 SkPath2DPathEffect::Create(matrix, path)); |
| 543 SkPaint paint; |
| 544 paint.setPathEffect(pathEffect); |
| 545 paint.setAntiAlias(true); |
| 546 canvas->clear(SK_ColorWHITE); |
| 547 SkRect bounds; |
| 548 (void)canvas->getClipBounds(&bounds); |
| 549 bounds.outset(2 * scale, 2 * scale); |
| 550 canvas->drawRect(bounds, paint); |
| 551 } |
| 552 |
| 553 <a href="https://fiddle.skia.org/c/aae271e4f0178455f0e128981d714d73"><img sr
c="https://fiddle.skia.org/i/aae271e4f0178455f0e128981d714d73_raster.png" alt=""
></a> |
| 554 |
| 555 * SkLine2DPathEffect: a special case of SkPath2DPathEffect where the |
| 556 path is a straight line to be stroked, not a path to be filled. |
| 557 |
| 558 <!--?prettify lang=cc?--> |
| 559 |
| 560 void draw(SkCanvas* canvas) { |
| 561 SkPaint paint; |
| 562 SkMatrix lattice; |
| 563 lattice.setScale(8.0f, 8.0f); |
| 564 lattice.preRotate(30.0f); |
| 565 SkAutoTUnref<SkPathEffect> pe( |
| 566 SkLine2DPathEffect::Create(0.0f, lattice)); |
| 567 paint.setPathEffect(pe); |
| 568 paint.setAntiAlias(true); |
| 569 SkRect bounds; |
| 570 (void)canvas->getClipBounds(&bounds); |
| 571 bounds.outset(8.0f, 8.0f); |
| 572 canvas->clear(SK_ColorWHITE); |
| 573 canvas->drawRect(bounds, paint); |
| 574 } |
| 575 |
| 576 <a href="https://fiddle.skia.org/c/3f49502145886920f95d43912e0f550d"><img sr
c="https://fiddle.skia.org/i/3f49502145886920f95d43912e0f550d_raster.png" alt=""
></a> |
| 577 |
| 578 * SkPath1DPathEffect: create dash-like effects by replicating the specified pa
th along the drawn path. |
| 579 |
| 580 <!--?prettify lang=cc?--> |
| 581 |
| 582 void draw(SkCanvas* canvas) { |
| 583 SkPaint paint; |
| 584 SkPath path; |
| 585 path.addOval(SkRect::MakeWH(16.0f, 6.0f)); |
| 586 SkAutoTUnref<SkPathEffect> pe( |
| 587 SkPath1DPathEffect::Create( |
| 588 path, 32.0f, 0.0f, SkPath1DPathEffect::kRotate_Style)); |
| 589 paint.setPathEffect(pe); |
| 590 paint.setAntiAlias(true); |
| 591 canvas->clear(SK_ColorWHITE); |
| 592 canvas->drawCircle(128.0f, 128.0f, 122.0f, paint); |
| 593 } |
| 594 |
| 595 <a href="https://fiddle.skia.org/c/756a8cdb9458c05f6c1c7c398d979dac"><img sr
c="https://fiddle.skia.org/i/756a8cdb9458c05f6c1c7c398d979dac_raster.png" alt=""
></a> |
| 596 |
| 597 * SkArcToPathEffect |
| 598 |
| 599 The following few examples use this function: |
| 600 |
| 601 <!--?prettify lang=cc?--> |
| 602 |
| 603 SkPath star() { |
| 604 const SkScalar R = 115.2f, C = 128.0f; |
| 605 SkPath path; |
| 606 path.moveTo(C + R, C); |
| 607 for (int i = 1; i < 8; ++i) { |
| 608 SkScalar a = 2.6927937f * i; |
| 609 path.lineTo(C + R * cos(a), C + R * sin(a)); |
| 610 } |
| 611 return path; |
| 612 } |
| 613 |
| 614 <!--?prettify lang=cc?--> |
| 615 |
| 616 void draw(SkCanvas* canvas) { |
| 617 SkPaint paint; |
| 618 SkAutoTUnref<SkPathEffect> pe( |
| 619 SkArcToPathEffect::Create(8.0f)); |
| 620 paint.setPathEffect(pe); |
| 621 paint.setStyle(SkPaint::kStroke_Style); |
| 622 paint.setAntiAlias(true); |
| 623 canvas->clear(SK_ColorWHITE); |
| 624 SkPath path(star()); |
| 625 canvas->drawPath(path, paint); |
| 626 } |
| 627 |
| 628 <a href="https://fiddle.skia.org/c/1cc2a1363dd0e96954e084f7ca29aa5f"><img sr
c="https://fiddle.skia.org/i/1cc2a1363dd0e96954e084f7ca29aa5f_raster.png" alt=""
></a> |
| 629 |
| 630 * SkCornerPathEffect: a path effect that can turn sharp corners into |
| 631 various treatments (e.g. rounded corners). |
| 632 |
| 633 <!--?prettify lang=cc?--> |
| 634 |
| 635 void draw(SkCanvas* canvas) { |
| 636 SkPaint paint; |
| 637 SkAutoTUnref<SkPathEffect> pe( |
| 638 SkCornerPathEffect::Create(32.0f)); |
| 639 paint.setPathEffect(pe); |
| 640 paint.setStyle(SkPaint::kStroke_Style); |
| 641 paint.setAntiAlias(true); |
| 642 canvas->clear(SK_ColorWHITE); |
| 643 const SkScalar R = 115.2f; |
| 644 SkPath path(star()); |
| 645 canvas->drawPath(path, paint); |
| 646 } |
| 647 |
| 648 <a href="https://fiddle.skia.org/c/272c7c17e295747338200ab62e2051e7"><img sr
c="https://fiddle.skia.org/i/272c7c17e295747338200ab62e2051e7_raster.png" alt=""
></a> |
| 649 |
| 650 * SkDashPathEffect: a path effect that implements dashing. |
| 651 |
| 652 <!--?prettify lang=cc?--> |
| 653 |
| 654 void draw(SkCanvas* canvas) { |
| 655 const SkScalar intervals[] = { 10.0f, 5.0f, 2.0f, 5.0f }; |
| 656 size_t count = sizeof(intervals) / sizeof(intervals[0]); |
| 657 SkAutoTUnref<SkPathEffect> pe( |
| 658 SkDashPathEffect::Create(intervals, count, 0.0f)); |
| 659 SkPaint paint; |
| 660 paint.setPathEffect(pe); |
| 661 paint.setStyle(SkPaint::kStroke_Style); |
| 662 paint.setStrokeWidth(2.0f); |
| 663 paint.setAntiAlias(true); |
| 664 canvas->clear(SK_ColorWHITE); |
| 665 SkPath path(star()); |
| 666 canvas->drawPath(path, paint); |
| 667 } |
| 668 |
| 669 <a href="https://fiddle.skia.org/c/d221ced999a80ac23870d0301ffeedad"><img sr
c="https://fiddle.skia.org/i/d221ced999a80ac23870d0301ffeedad_raster.png" alt=""
></a> |
| 670 |
| 671 * SkDiscretePathEffect: This path effect chops a path into discrete |
| 672 segments, and randomly displaces them. |
| 673 |
| 674 <!--?prettify lang=cc?--> |
| 675 |
| 676 void draw(SkCanvas* canvas) { |
| 677 SkAutoTUnref<SkPathEffect> pe( |
| 678 SkDiscretePathEffect::Create(10.0f, 4.0f)); |
| 679 SkPaint paint; |
| 680 paint.setPathEffect(pe); |
| 681 paint.setStyle(SkPaint::kStroke_Style); |
| 682 paint.setStrokeWidth(2.0f); |
| 683 paint.setAntiAlias(true); |
| 684 canvas->clear(SK_ColorWHITE); |
| 685 SkPath path(star()); |
| 686 canvas->drawPath(path, paint); |
| 687 } |
| 688 |
| 689 <a href="https://fiddle.skia.org/c/af2f177438b376ca45cfffc29cc0177a"><img sr
c="https://fiddle.skia.org/i/af2f177438b376ca45cfffc29cc0177a_raster.png" alt=""
></a> |
| 690 |
| 691 * SkComposePathEffect: a pathEffect whose effect is to apply |
| 692 first the inner pathEffect and the the outer pathEffect (i.e. |
| 693 outer(inner(path))). |
| 694 |
| 695 <!--?prettify lang=cc?--> |
| 696 |
| 697 void draw(SkCanvas* canvas) { |
| 698 SkAutoTUnref<SkPathEffect> pe0( |
| 699 SkDiscretePathEffect::Create(10.0f, 4.0f)); |
| 700 const SkScalar intervals[] = { 10.0f, 5.0f, 2.0f, 5.0f }; |
| 701 size_t count = sizeof(intervals) / sizeof(intervals[0]); |
| 702 SkAutoTUnref<SkPathEffect> pe1( |
| 703 SkDashPathEffect::Create(intervals, count, 0.0f)); |
| 704 SkAutoTUnref<SkPathEffect> pe( |
| 705 SkComposePathEffect::Create(pe1, pe0)); |
| 706 SkPaint paint; |
| 707 paint.setPathEffect(pe); |
| 708 paint.setStyle(SkPaint::kStroke_Style); |
| 709 paint.setStrokeWidth(2.0f); |
| 710 paint.setAntiAlias(true); |
| 711 canvas->clear(SK_ColorWHITE); |
| 712 SkPath path(star()); |
| 713 canvas->drawPath(path, paint); |
| 714 } |
| 715 |
| 716 <a href="https://fiddle.skia.org/c/39a644161da79e8b5e49c193adac7173"><img sr
c="https://fiddle.skia.org/i/39a644161da79e8b5e49c193adac7173_raster.png" alt=""
></a> |
| 717 |
| 718 * SkSumPathEffect: a pathEffect whose effect is to apply two effects, |
| 719 in sequence (i.e. first(path) + second(path)). |
| 720 |
| 721 <!--?prettify lang=cc?--> |
| 722 |
| 723 void draw(SkCanvas* canvas) { |
| 724 SkAutoTUnref<SkPathEffect> pe0( |
| 725 SkDiscretePathEffect::Create(10.0f, 4.0f)); |
| 726 SkAutoTUnref<SkPathEffect> pe1( |
| 727 SkDiscretePathEffect::Create(10.0f, 4.0f, 1245u)); |
| 728 SkAutoTUnref<SkPathEffect> pe( |
| 729 SkSumPathEffect::Create(pe1, pe0)); |
| 730 SkPaint paint; |
| 731 paint.setPathEffect(pe); |
| 732 paint.setStyle(SkPaint::kStroke_Style); |
| 733 paint.setStrokeWidth(2.0f); |
| 734 paint.setAntiAlias(true); |
| 735 canvas->clear(SK_ColorWHITE); |
| 736 SkPath path(star()); |
| 737 canvas->drawPath(path, paint); |
| 738 } |
| 739 |
| 740 <a href="https://fiddle.skia.org/c/e5f7861072893bd08c305a076bf32958"><img sr
c="https://fiddle.skia.org/i/e5f7861072893bd08c305a076bf32958_raster.png" alt=""
></a> |
| 741 |
| 742 <!-- |
| 743 <a href="https://fiddle.skia.org/c/"><img src="https://fiddle.skia.org/i/_ra
ster.png" alt=""></a> |
| 744 --> |
| 745 |
OLD | NEW |