Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates #1

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
Glitch effects to images.

- basic

![basic_](https://user-images.githubusercontent.com/9162117/183775728-77000c77-73aa-4395-9b58-0abff74751ea.gif)

- highFrequency

![highFrequency_](https://user-images.githubusercontent.com/9162117/183775762-64edf697-2c27-4431-8208-935b631b99de.gif)


Expand All @@ -26,11 +28,6 @@ dependencies:

```dart
FGlitch(
imageProvider: Image.asset('assets/sample.jpg').image
imageProvider: const AssetImage('assets/sample.jpg')
);
```

# others
## used unsplash images
- https://unsplash.com/photos/ce-K_rj1P7U
- https://unsplash.com/photos/M6ule9BFwYg
9 changes: 4 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
ImageProvider _imageProvider = Image.asset('assets/sample.jpg').image;
ImageProvider _imageProvider = const AssetImage('assets/sample.jpg');
double _frequency = 1000;
double _glitchRate = 50;

Expand Down Expand Up @@ -58,7 +58,7 @@ class _MyHomePageState extends State<MyHomePage> {
ElevatedButton(
onPressed: () {
setState(() {
_imageProvider = Image.asset('assets/sample.jpg').image;
_imageProvider = const AssetImage('assets/sample.jpg');
});
},
child: const Text('local image'),
Expand All @@ -69,9 +69,8 @@ class _MyHomePageState extends State<MyHomePage> {
ElevatedButton(
onPressed: () {
setState(() {
_imageProvider =
Image.network("https://source.unsplash.com/M6ule9BFwYg")
.image;
_imageProvider = const NetworkImage(
"https://source.unsplash.com/M6ule9BFwYg");
});
},
child: const Text('network image'),
Expand Down
31 changes: 12 additions & 19 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,21 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -63,7 +56,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -94,28 +87,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
plugin_platform_interface:
dependency: transitive
description:
Expand All @@ -134,7 +127,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -155,21 +148,21 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
version: "0.4.12"
vector_math:
dependency: transitive
description:
Expand Down
78 changes: 56 additions & 22 deletions lib/f_glitch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

/// A widget that renders an glitched image.
/// Effect has a [glitchRate]% chance of occurring every [frequency] milliseconds.
class FGlitch extends StatefulWidget {
FGlitch(
{Key? key,
Expand All @@ -22,19 +24,26 @@ class FGlitch extends StatefulWidget {
.toList();
}


/// Interval that happens effect.
final int frequency; // milliseconds

/// Rate that happens effect.
final int glitchRate; // 1 - 100

/// A image that is used to effected.
final ImageProvider imageProvider;

late final List<_ColorChannel> _colorChannels;
late final List<_GlitchMask> _glitchList;

final List<Color> _defaultChannelColors = [
final List<Color> _defaultChannelColors = const [
Colors.red,
Colors.green,
Colors.blue,
];

final List<BlendMode> _defaultGlitchList = [
final List<BlendMode> _defaultGlitchList = const [
BlendMode.softLight,
BlendMode.multiply,
];
Expand All @@ -46,18 +55,30 @@ class FGlitch extends StatefulWidget {
class _FGlitchState extends State<FGlitch> {
late int _frequency;
final _key = GlobalKey();
static final Random _random = Random();
List<Timer> _timers = [];

@override
void initState() {
super.initState();

_frequency = widget.frequency;
_fGlitchTimer();
}

super.initState();

@override
void dispose() {
for (var timer in _timers) {
timer.cancel();
}
_timers = [];
super.dispose();
}

void _fGlitchTimer() {
Timer.periodic(Duration(milliseconds: widget.frequency), (Timer timer) {
if (Random().nextInt(100) > widget.glitchRate) return;
final timer = Timer.periodic(Duration(milliseconds: widget.frequency), (Timer timer) {
if (_random.nextInt(100) > widget.glitchRate) return;

_setTimer(
_onTimerColorChannelShift(widget._colorChannels
Expand Down Expand Up @@ -93,13 +114,15 @@ class _FGlitchState extends State<FGlitch> {
_fGlitchTimer();
}
});
_timers.add(timer);
}

void _setTimer(void Function(Timer) fn, int milliseconds) {
Timer.periodic(
final timer = Timer.periodic(
Duration(milliseconds: milliseconds),
fn,
);
_timers.add(timer);
}

void Function(Timer) _onTimerColorChannelShift(
Expand Down Expand Up @@ -129,7 +152,7 @@ class _FGlitchState extends State<FGlitch> {
widget._glitchList[key] = glitchMask;
});

Timer.periodic(
final glitchTimer = Timer.periodic(
const Duration(milliseconds: 300),
(Timer timer) {
timer.cancel();
Expand All @@ -141,18 +164,19 @@ class _FGlitchState extends State<FGlitch> {
});
},
);
_timers.add(glitchTimer);
};
}

EdgeInsets _randomSideMargin(double min, double max) {
var side = Random().nextDouble() * (max - min) + min;
var side = _random.nextDouble() * (max - min) + min;
return side < 0
? EdgeInsets.only(right: side * -1)
: EdgeInsets.only(left: side);
}

double _randomPosition(double min, double max) {
return Random().nextDouble() * (max - min) + min;
return _random.nextDouble() * (max - min) + min;
}

Widget _channelWidget(_ColorChannel cc) {
Expand Down Expand Up @@ -215,10 +239,10 @@ class _FGlitchState extends State<FGlitch> {
}

class _InvertedCircleClipper extends CustomClipper<Path> {
_InvertedCircleClipper(this._top, this._heightRate);
const _InvertedCircleClipper(this._top, this._heightRate);

late final double _top;
late final double _heightRate;
final double _top;
final double _heightRate;

@override
Path getClip(Size size) {
Expand All @@ -234,17 +258,22 @@ class _InvertedCircleClipper extends CustomClipper<Path> {
}

class _ColorChannel {
_ColorChannel(this._color, {this.topPosition = 0, this.leftPosition = 0});
const _ColorChannel(this._color,
{this.topPosition = 0, this.leftPosition = 0});

final Color _color;

/// [_ColorChannel]'s top position.
final double topPosition;

late final Color _color;
double topPosition;
double leftPosition;
/// [_ColorChannel]'s left position.
final double leftPosition;
}

class _GlitchMask {
_GlitchMask(this._blendMode);

late final BlendMode _blendMode;
final BlendMode _blendMode;
EdgeInsets? _leftMargin;
double _topPosition = 0;
double _heightRate = 1;
Expand All @@ -262,15 +291,17 @@ class _GlitchMask {
}

class _BlendMask extends SingleChildRenderObjectWidget {
final BlendMode blendMode;
final double _opacity = 1.0;

const _BlendMask({
required this.blendMode,
Key? key,
Widget? child,
}) : super(key: key, child: child);

/// A widget that renders an glitched image.
final BlendMode blendMode;

final double _opacity = 1.0;

@override
RenderObject createRenderObject(context) {
return _RenderBlendMask(blendMode, _opacity);
Expand All @@ -284,10 +315,13 @@ class _BlendMask extends SingleChildRenderObjectWidget {
}

class _RenderBlendMask extends RenderProxyBox {
_RenderBlendMask(this.blendMode, this.opacity);

/// ColorChannels blend mode.
BlendMode blendMode;
double opacity;

_RenderBlendMask(this.blendMode, this.opacity);
/// ColorChannels opacity.
double opacity;

@override
void paint(context, offset) {
Expand Down
6 changes: 4 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: f_glitch
description: Add glitch effect and RGB shift effect to selected image and change frequency of occurrence to these effects.
version: 0.0.1
description: >
Add glitch effect and RGB shift effect to selected image and
change frequency of occurrence to these effects.
version: 0.0.2
homepage: https://github.com/kyu-suke/f_glitch

environment:
Expand Down