Skip to content

Commit

Permalink
Add examples when cropping images, Add generation default values of m…
Browse files Browse the repository at this point in the history
…ethod parameters for API (#263)

- add generation default values of method parameters for API

## Target
<!--
  Why are you making this change?
 -->
Add examples when cropping images
Add generation default values of method parameters for API
#### Open Questions
<!-- OPTIONAL
- [ ] Use the GitHub checklists to spark discussion on issues that may
arise from your approach. Please tick the box and explain your answer.
-->

## Checklist
<!--
It serves as a gentle reminder for common tasks. Confirm it's done and
check everything that applies.
-->
- [x] Documentation updated
- [x] Tests cover new or modified code
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] New dependencies added
- [ ] Includes breaking changes
- [ ] Version bumped

## Visuals
<!-- OPTIONAL
Show results both before and after this change. When the output changes,
it can be a screenshot of a trace, metric, or log illustrating the
change.
-->
  • Loading branch information
MaxymGorn authored Nov 15, 2023
1 parent 9f40e96 commit a1530d1
Show file tree
Hide file tree
Showing 40 changed files with 750 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/Cropper.Blazor/Client/Components/Docs/DocsApi.razor
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</MudTd>
<MudTd Class="docs-content-api-cell" DataLabel="Default">
@{
var def = PresentDefaultValue(context.Default);
var def = context.Default.PresentDefaultValue();
}
@if (def.Contains("<path"))
{
Expand Down
35 changes: 1 addition & 34 deletions src/Cropper.Blazor/Client/Components/Docs/DocsApi.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Reflection;
using System.Reflection;
using Cropper.Blazor.Client.Models;
using Cropper.Blazor.Events;
using Cropper.Blazor.Models;
Expand Down Expand Up @@ -257,38 +256,6 @@ private object GetDefaultValue(PropertyInfo info)
return info.GetValue(_comp_instance);
}

DefaultConverter<object> _converter = new DefaultConverter<object>()
{
Culture = CultureInfo.InvariantCulture
};

private string PresentDefaultValue(object value)
{
if (value == null)
return "null";
if (value.GetType() == typeof(string))
{
if (value.ToString() == string.Empty)
{
return "";
}
else
{
return $"\"{value}\"";
}
}
if (value.GetType().IsEnum)
return $"{value.GetType().Name}.{value}";
if (Nullable.GetUnderlyingType(value.GetType()) != null)
return _converter.Set(value);
if (value.GetType().IsGenericType) // for instance event callbacks
return "";
if (value.GetType().IsValueType)
return _converter.Set(value);
return "";
}


#region Grouping properties

private enum Grouping { Categories, Inheritance, None }
Expand Down
3 changes: 2 additions & 1 deletion src/Cropper.Blazor/Client/Cropper.Blazor.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.13" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.13" PrivateAssets="all" />
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="MudBlazor" Version="6.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -184,6 +183,8 @@
</ItemGroup>
<ItemGroup>
<None Remove="Pages\CropZoom\Examples\MinMaxZoomRatio_ScriptCode.html" />
<None Remove="Pages\Crop\Examples\CropPolygonImage_ScriptCode.html" />
<None Remove="Pages\Crop\Examples\CropRoundImage_ScriptCode.html" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cropper.Blazor.Shared\Cropper.Blazor.Shared.csproj" />
Expand Down
9 changes: 6 additions & 3 deletions src/Cropper.Blazor/Client/Pages/About.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
<MudCard Elevation="4">
<MudCardHeader>
<CardHeaderAvatar>
<MudAvatar Image="@teamMember.Avatar"
Size="Size.Large"
Class="mud-elevation-4" />
<MudAvatar Size="Size.Large"
Class="mud-elevation-4">
<MudImage UserAttributes="@AvatarInputAttributes" Src="@teamMember.Avatar">

</MudImage>
</MudAvatar>
</CardHeaderAvatar>
<CardHeaderContent>
<MudText Typo="Typo.h6">
Expand Down
7 changes: 7 additions & 0 deletions src/Cropper.Blazor/Client/Pages/About.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ public partial class About
Languages = "English, Ukrainian"
}
};

public Dictionary<string, object> AvatarInputAttributes { get; set; } =
new Dictionary<string, object>()
{
{ "loading", "lazy" },
{ "alt", "GitHub avatar image" }
};
}
}
20 changes: 19 additions & 1 deletion src/Cropper.Blazor/Client/Pages/Crop/CropPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,23 @@
</SectionContent>
</DocsPageSection>

<DocsPageSection>
<SectionHeader Title="Crop a round image">

</SectionHeader>
<SectionContent Codes="@(new[] {new CodeFile("Cropper.razor", "CropRoundImageExample"), new CodeFile("script.js", "CropRoundImage_Script")})">
<CropRoundImageExample />
</SectionContent>
</DocsPageSection>

<DocsPageSection>
<SectionHeader Title="Crop a polygon image">

</SectionHeader>
<SectionContent Codes="@(new[] {new CodeFile("Cropper.razor", "CropPolygonImageExample"), new CodeFile("script.js", "CropPolygonImage_Script")})">
<CropPolygonImageExample />
</SectionContent>
</DocsPageSection>

</DocsPageContent>
</DocsPage>
</DocsPage>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@using Cropper.Blazor.Models;

<div class="img-container">
<CropperComponent Class="big-img" Src="images/Rivne.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
<CropperComponent Class="big-img" Src="images/budir-church-bu-akirkja-iceland.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasAsync">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@using Cropper.Blazor.Models;

<div class="img-container">
<CropperComponent Class="big-img" Src="images/Rivne.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
<CropperComponent Class="big-img" Src="images/budir-church-bu-akirkja-iceland.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasDataURLAsync">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@using Cropper.Blazor.Extensions;
@using Cropper.Blazor.Models;

<div class="img-container cropper-face-pentagon">
<CropperComponent Class="big-img" Src="images/Mushrooms.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasAsync">
Get cropped image
</div>

<img class="cropped-img-container" src="@croppedCanvasDataURL" />

@* Make sure the size of the image fits perfectly into the container *@
<style>
.cropper-face {
opacity: 25%;
}
.img-container.cropper-face-pentagon .cropper-container .cropper-crop-box .cropper-face {
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
}
.big-img {
max-height: 400px;
/* This rule is very important, please don't ignore this */
max-width: 100%;
}
.img-container {
max-height: 400px;
width: 100%;
}
/* Means that the cropped image will take up 100% of the width of its containing element */
.cropped-img-container {
width: 100%;
}
/* These styles are just needed for a nice button and don't related with cropper component */
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
text-align: center;
text-decoration: none;
font-size: 16px;
cursor: pointer;
}
</style>

@code {
[Inject] private IJSRuntime? JSRuntime { get; set; }

private CropperComponent? cropperComponent = null!;
private string croppedCanvasDataURL;

public async Task GetCroppedCanvasAsync()
{
GetCroppedCanvasOptions getCroppedCanvasOptions = new GetCroppedCanvasOptions
{
MaxHeight = 4096,
MaxWidth = 4096,
ImageSmoothingQuality = ImageSmoothingQuality.High.ToEnumString()
};

// Get a reference to a JavaScript cropped canvas object.
CroppedCanvas croppedCanvas = await cropperComponent!.GetCroppedCanvasAsync(getCroppedCanvasOptions);
// Invoke toDataURL JavaScript function from the canvas object.
croppedCanvasDataURL = await JSRuntime!.InvokeAsync<string>(
"window.getPolygonImage",
croppedCanvas!.JSRuntimeObjectRef,
// Defines a polygon using an SVG fill rule and a set of vertices previously defined in the following format styles:
// .img-container.cropper-face-pentagon.cropper-container.cropper-crop-box.cropper-face {
// clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
// }
// In our case, we need to pass the same data, but without the percent sign (%)
new int[] { 50, 0, 100, 38, 82, 100, 18, 100, 0, 38 });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@namespace Cropper.Blazor.Client.Pages.Crop.Examples

@* Dummy file do not remove, needed for content section *@
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div class="mud-codeblock">
<div class="html">
<pre>
<span class="htmlTagDelimiter">&lt;</span><span class="htmlElementName">script</span><span class="htmlTagDelimiter">&gt;</span>
window.getPolygonImage = (sourceCanvas, path) => {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const width = sourceCanvas.width,
height = sourceCanvas.height;

canvas.width = width;
canvas.height = height;
context.imageSmoothingEnabled = true;

context.beginPath();
context.moveTo(path[0] * width / 100, path[1] * height / 100);
context.fillStyle = "rgba(255, 255, 255, 0)";

for (let i = 2; i < path.length; i += 2) {
context.lineTo(path[i] * width / 100, path[i + 1] * height / 100);
}

context.closePath();
context.clip();
context.fill();
context.globalCompositeOperation = 'lighter';
context.drawImage(sourceCanvas, 0, 0, width, height);

return canvas.toDataURL("image/png", 1);
}
<span class="htmlTagDelimiter">&lt;&#47;</span><span class="htmlElementName">script</span> <span class="htmlTagDelimiter">&gt;</span>
</pre>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@using Cropper.Blazor.Extensions;
@using Cropper.Blazor.Models;

<div class="img-container cropper-face-circle">
<CropperComponent Class="big-img" Src="images/lone-tree.jpg" @ref="cropperComponent" Options="new Blazor.Models.Options()" />
</div>

<div class="button" @onclick="GetCroppedCanvasAsync">
Get cropped image
</div>

<img class="cropped-img-container" src="@croppedCanvasDataURL" />

@* Make sure the size of the image fits perfectly into the container *@
<style>
.cropper-face {
opacity: 25%;
}
.img-container.cropper-face-circle .cropper-container .cropper-crop-box .cropper-face {
border-radius: 50%;
}
.big-img {
max-height: 400px;
/* This rule is very important, please don't ignore this */
max-width: 100%;
}
.img-container {
max-height: 400px;
width: 100%;
}
/* Means that the cropped image will take up 100% of the width of its containing element */
.cropped-img-container {
width: 100%;
}
/* These styles are just needed for a nice button and don't related with cropper component */
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
text-align: center;
text-decoration: none;
font-size: 16px;
cursor: pointer;
}
</style>

@code {
[Inject] private IJSRuntime? JSRuntime { get; set; }

private CropperComponent? cropperComponent = null!;
private string croppedCanvasDataURL;

public async Task GetCroppedCanvasAsync()
{
GetCroppedCanvasOptions getCroppedCanvasOptions = new GetCroppedCanvasOptions
{
MaxHeight = 4096,
MaxWidth = 4096,
ImageSmoothingQuality = ImageSmoothingQuality.High.ToEnumString()
};

// Get a reference to a JavaScript cropped canvas object.
CroppedCanvas croppedCanvas = await cropperComponent!.GetCroppedCanvasAsync(getCroppedCanvasOptions);
// Invoke toDataURL JavaScript function from the canvas object.
croppedCanvasDataURL = await JSRuntime!.InvokeAsync<string>("window.getEllipseImage", croppedCanvas!.JSRuntimeObjectRef);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@namespace Cropper.Blazor.Client.Pages.Crop.Examples

@* Dummy file do not remove, needed for content section *@
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="mud-codeblock">
<div class="html">
<pre>
<span class="htmlTagDelimiter">&lt;</span><span class="htmlElementName">script</span><span class="htmlTagDelimiter">&gt;</span>
window.getEllipseImage = (sourceCanvas) => {
const createdCanvas = document.createElement('canvas');
const contextCanvas = createdCanvas.getContext('2d');
const widthCanvas = sourceCanvas.width,
heightCanvas = sourceCanvas.height;

createdCanvas.width = widthCanvas;
createdCanvas.height = heightCanvas;
contextCanvas.imageSmoothingEnabled = true;

contextCanvas.drawImage(sourceCanvas, 0, 0, widthCanvas, heightCanvas);
contextCanvas.globalCompositeOperation = 'destination-in';
contextCanvas.beginPath();
contextCanvas.ellipse(widthCanvas / 2, heightCanvas / 2, widthCanvas / 2, heightCanvas / 2, 0 * Math.PI, 0, 180 * Math.PI, true);
contextCanvas.fill();

return createdCanvas.toDataURL("image/png", 1);
}
<span class="htmlTagDelimiter">&lt;&#47;</span><span class="htmlElementName">script</span> <span class="htmlTagDelimiter">&gt;</span>
</pre>
</div>
</div>
4 changes: 2 additions & 2 deletions src/Cropper.Blazor/Client/Pages/CropperDemo.razor
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@
<MudIconButton Icon="@Icons.Material.Filled.Sync" OnClick="Reset" Title="Reset" />
</MudTooltip>
<MudTooltip Text="Import image with Blob URLs">
<InputFile id="imageInput" OnChange="InputFileChangeAsync" accept="image/*" hidden multiple />
<InputFile id="imageInput" OnChange="InputFileChangeAsync" accept="image/*" hidden />
<MudIconButton HtmlTag="label" Icon="@Icons.Material.Filled.PhotoCamera" for="imageInput" Title="Upload image file" />
</MudTooltip>
<MudTooltip Text="Replace image with Blob URLs">
<InputFile id="imageReplaceInput" OnChange="ReplaceImageAsync" accept="image/*" hidden multiple />
<InputFile id="imageReplaceInput" OnChange="ReplaceImageAsync" accept="image/*" hidden />
<MudIconButton HtmlTag="label" Icon="@Icons.Material.Filled.FindReplace" for="imageReplaceInput" Title="Replace image file" />
</MudTooltip>
<MudTooltip Text="cropper.destroy()">
Expand Down
Loading

0 comments on commit a1530d1

Please sign in to comment.