Skip to content

Commit

Permalink
Merge pull request #22 from zats/master
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldeicaza authored Feb 25, 2021
2 parents e5f3649 + 9ec64c4 commit 0c6a4f6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
72 changes: 72 additions & 0 deletions Samples/Gallery/AnimationSample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// AnimationSample.swift
// SkiaSamplesiOS
//
// Created by Sash Zats on 12/13/20.
//

import Foundation
import SkiaKit

struct animationSample : Sample {
static var title = "Animation"

var isLoop: Bool { true }

class AnimationViewModel {
private let circleRadius: Float = 100
private lazy var position = Point(x: circleRadius, y: circleRadius)
private var velocity = Point(x: 10, y: 10)
private var bounds = Rect()

func update(width: Float, height: Float) {
self.bounds = Rect(x: 0,
y: 0,
width: Float(width) - circleRadius * 2,
height: Float(height) - circleRadius * 2)

var nextPosition = Point(x: position.x + velocity.x,
y: position.y + velocity.y)
if nextPosition.x < bounds.left || nextPosition.x > bounds.right {
velocity.x *= -1
nextPosition = Point(x: position.x + velocity.x,
y: position.y + velocity.y)
}

if nextPosition.y < bounds.top || nextPosition.y > bounds.bottom {
velocity.y *= -1;
nextPosition = Point(x: position.x + velocity.x,
y: position.y + velocity.y)
}

position = nextPosition
}

var ovalBounds: Rect {
return Rect(x: position.x, y: position.y, width: circleRadius * 2, height: circleRadius * 2)
}
}

let viewModel = AnimationViewModel()

func draw(canvas: Canvas, width: Int32, height: Int32)
{
viewModel.update(width: Float(width), height: Float(height))

canvas.clear (color: Colors.white)

let paint = Paint()
paint.isAntialias = true
paint.color = Colors.darkGray
paint.strokeCap = .round
paint.strokeWidth = 10
paint.isStroke = true

let path = SkiaKit.Path()
path.addOval(viewModel.ovalBounds)
path.close()

canvas.drawPath(path, paint)

}
}
8 changes: 8 additions & 0 deletions Samples/Gallery/Sample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ protocol Sample {
init ()

static var title: String { get }

var isLoop: Bool { get }

//var category: String { get } =
func draw (canvas: Canvas, width: Int32, height: Int32)
}

extension Sample {
var isLoop: Bool { return false }
}

struct SampleRender : UIViewRepresentable {
var sample: Sample
init (_ sample: Sample)
Expand All @@ -31,6 +38,7 @@ struct SampleRender : UIViewRepresentable {
func makeUIView (context: Context) -> SkiaView
{
let sv = SkiaView ()
sv.loop = sample.isLoop
sv.drawingCallback = draw

return sv
Expand Down
1 change: 1 addition & 0 deletions Samples/SkiaSamplesiOS/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct SampleChooserView : View {
Display<samplePathBounds>()
Display<sampleText>()
Display<sampleXamagon>()
Display<animationSample>()
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions SkiaKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
49E975A72367ACAD00042FE7 /* Pixmap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E975A62367ACAD00042FE7 /* Pixmap.swift */; };
49E975A82367ACAD00042FE7 /* Pixmap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E975A62367ACAD00042FE7 /* Pixmap.swift */; };
49E975A92367ACAD00042FE7 /* Pixmap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E975A62367ACAD00042FE7 /* Pixmap.swift */; };
5C8058C62586C5E300B8D052 /* AnimationSample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C8058C52586C5E300B8D052 /* AnimationSample.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -432,6 +433,7 @@
49E9756F2366821800042FE7 /* TextSample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextSample.swift; sourceTree = "<group>"; };
49E97571236683E900042FE7 /* XamagonSample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XamagonSample.swift; sourceTree = "<group>"; };
49E975A62367ACAD00042FE7 /* Pixmap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pixmap.swift; sourceTree = "<group>"; };
5C8058C52586C5E300B8D052 /* AnimationSample.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = AnimationSample.swift; sourceTree = "<group>"; tabWidth = 4; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -757,6 +759,7 @@
4902C78E23765A6300FD5CE3 /* FilledHeptagramSample.swift */,
4902C7902376644300FD5CE3 /* 2DPathSample.swift */,
4902C7922376647F00FD5CE3 /* SimplePathTest.swift */,
5C8058C52586C5E300B8D052 /* AnimationSample.swift */,
);
path = Gallery;
sourceTree = "<group>";
Expand Down Expand Up @@ -1308,6 +1311,7 @@
4902C78D23765A3B00FD5CE3 /* FractalPerlinNoiseShaderSample.swift in Sources */,
49E9752E2365360D00042FE7 /* ContentView.swift in Sources */,
49E97572236683E900042FE7 /* XamagonSample.swift in Sources */,
5C8058C62586C5E300B8D052 /* AnimationSample.swift in Sources */,
4902C78F23765A6300FD5CE3 /* FilledHeptagramSample.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
28 changes: 27 additions & 1 deletion SkiaKit/Apple/SkiaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ public class SkiaView: UIView {
/// This property when set points to the method to invoke when drawing. The method
/// receives a surface and the ImageInfo where it should draw its contents.
public var drawingCallback: (_ surface: Surface, _ imageInfo: ImageInfo) -> () = emptyCallback(surface:imageInfo:)

private lazy var displayLink: CADisplayLink =
{
let link = CADisplayLink(target: self, selector: #selector(onDisplayLink(_:)))
link.isPaused = true
return link
}()

private func commonInit()
{
displayLink.add(to: .main, forMode: .common)
}

public var loop: Bool = false
{
didSet {
displayLink.isPaused = !loop
}
}

@objc private func onDisplayLink(_ sender: CADisplayLink)
{
setNeedsDisplay()
}

static func emptyCallback (surface: Surface, imageInfo: ImageInfo)
{
Expand All @@ -37,16 +61,18 @@ public class SkiaView: UIView {
override init(frame: CGRect)
{
super.init(frame: frame)
commonInit()
}

required init? (coder: NSCoder)
{
super.init (coder: coder)
commonInit()
}

override public func draw(_ rect: CGRect) {
super.draw (rect)

// Create the Skia Context
let scale = ignorePixelScaling ? 1 : contentScaleFactor
let info = ImageInfo(width: Int32 (bounds.width * scale), height: Int32 (bounds.height * scale), colorType: .bgra8888, alphaType: .premul)
Expand Down

0 comments on commit 0c6a4f6

Please sign in to comment.